@@ -50,43 +50,48 @@ def request(method, url, data=None, json=None, headers={}, stream=None):
50
50
51
51
ai = usocket .getaddrinfo (host , port )
52
52
addr = ai [0 ][- 1 ]
53
+
53
54
s = usocket .socket ()
54
- s .connect (addr )
55
- if proto == "https:" :
56
- s = ussl .wrap_socket (s , server_hostname = host )
57
- s .write (b"%s /%s HTTP/1.0\r \n " % (method , path ))
58
- if not "Host" in headers :
59
- s .write (b"Host: %s\r \n " % host )
60
- # Iterate over keys to avoid tuple alloc
61
- for k in headers :
62
- s .write (k )
63
- s .write (b": " )
64
- s .write (headers [k ])
55
+ try :
56
+ s .connect (addr )
57
+ if proto == "https:" :
58
+ s = ussl .wrap_socket (s , server_hostname = host )
59
+ s .write (b"%s /%s HTTP/1.0\r \n " % (method , path ))
60
+ if not "Host" in headers :
61
+ s .write (b"Host: %s\r \n " % host )
62
+ # Iterate over keys to avoid tuple alloc
63
+ for k in headers :
64
+ s .write (k )
65
+ s .write (b": " )
66
+ s .write (headers [k ])
67
+ s .write (b"\r \n " )
68
+ if json is not None :
69
+ assert data is None
70
+ import ujson
71
+ data = ujson .dumps (json )
72
+ if data :
73
+ s .write (b"Content-Length: %d\r \n " % len (data ))
65
74
s .write (b"\r \n " )
66
- if json is not None :
67
- assert data is None
68
- import ujson
69
- data = ujson .dumps (json )
70
- if data :
71
- s .write (b"Content-Length: %d\r \n " % len (data ))
72
- s .write (b"\r \n " )
73
- if data :
74
- s .write (data )
75
-
76
- l = s .readline ()
77
- protover , status , msg = l .split (None , 2 )
78
- status = int (status )
79
- #print(protover, status, msg)
80
- while True :
75
+ if data :
76
+ s .write (data )
77
+
81
78
l = s .readline ()
82
- if not l or l == b"\r \n " :
83
- break
84
- #print(l)
85
- if l .startswith (b"Transfer-Encoding:" ):
86
- if b"chunked" in l :
87
- raise ValueError ("Unsupported " + l )
88
- elif l .startswith (b"Location:" ) and not 200 <= status <= 299 :
89
- raise NotImplementedError ("Redirects not yet supported" )
79
+ protover , status , msg = l .split (None , 2 )
80
+ status = int (status )
81
+ #print(protover, status, msg)
82
+ while True :
83
+ l = s .readline ()
84
+ if not l or l == b"\r \n " :
85
+ break
86
+ #print(l)
87
+ if l .startswith (b"Transfer-Encoding:" ):
88
+ if b"chunked" in l :
89
+ raise ValueError ("Unsupported " + l )
90
+ elif l .startswith (b"Location:" ) and not 200 <= status <= 299 :
91
+ raise NotImplementedError ("Redirects not yet supported" )
92
+ except OSError :
93
+ s .close ()
94
+ raise
90
95
91
96
resp = Response (s )
92
97
resp .status_code = status
0 commit comments