Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/python-wasm/data/pip/scratch.py
1067 views
1
"""
2
3
"""
4
5
import socket
6
7
def _set_socket_options(sock, options):
8
if options is None:
9
return
10
11
for opt in options:
12
sock.setsockopt(*opt)
13
14
15
def f(address):
16
timeout = 5
17
socket_options = [(6, 1, 1)]
18
19
host, port = address
20
if host.startswith("["):
21
host = host.strip("[]")
22
err = None
23
24
# Using the value from allowed_gai_family() in the context of getaddrinfo lets
25
# us select whether to work with IPv4 DNS records, IPv6 records, or both.
26
# The original create_connection function always returns all records.
27
family = socket.AF_INET
28
29
host.encode("idna")
30
31
for res in socket.getaddrinfo("pypi.org", 443, family, socket.SOCK_STREAM):
32
print("res = ", res)
33
af, socktype, proto, canonname, sa = res
34
sock = socket.socket(af, socktype, proto)
35
_set_socket_options(sock, socket_options)
36
sock.settimeout(timeout)
37
print("sock.connect(sa) ", sock, sa)
38
sock.connect(sa)
39
print("connected!", sock)
40
return sock
41
42
43
f(['pypy.org', 443])
44
45