-
Notifications
You must be signed in to change notification settings - Fork 2
/
0008-Set-timeout-to-http.client.HTTPConnection.patch
95 lines (85 loc) · 3.53 KB
/
0008-Set-timeout-to-http.client.HTTPConnection.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
From c0f755315efffa27c32855fe4d920f182e71867d Mon Sep 17 00:00:00 2001
From: mozbugbox <mozbugbox@yahoo.com.au>
Date: Wed, 16 Mar 2016 21:40:26 +0800
Subject: [PATCH 08/10] Set timeout to http.client.HTTPConnection
Without a timeout, upload could block like hell.
---
bcloud/net.py | 23 +++++++++++++----------
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/bcloud/net.py b/bcloud/net.py
index 2305a48..6c0c222 100644
--- a/bcloud/net.py
+++ b/bcloud/net.py
@@ -30,7 +30,7 @@ default_headers = {
'Cache-control': 'no-cache',
}
-def urloption(url, headers={}, retries=RETRIES):
+def urloption(url, headers={}, retries=RETRIES, timeout=TIMEOUT):
'''发送OPTION 请求'''
headers_merged = default_headers.copy()
for key in headers.keys():
@@ -38,7 +38,7 @@ def urloption(url, headers={}, retries=RETRIES):
schema = urllib.parse.urlparse(url)
for i in range(retries):
try:
- conn = http.client.HTTPConnection(schema.netloc)
+ conn = http.client.HTTPConnection(schema.netloc, timeout=timeout)
conn.request('OPTIONS', url, headers=headers_merged)
resp = conn.getresponse()
return resp
@@ -66,10 +66,10 @@ def urlopen_simple(url, retries=RETRIES, timeout=TIMEOUT):
return urllib.request.urlopen(url, timeout=timeout)
except OSError:
logger.error(traceback.format_exc())
-
+
except :
logger.error(traceback.format_exc())
-
+
return None
def urlopen(url, headers={}, data=None, retries=RETRIES, timeout=TIMEOUT):
@@ -101,13 +101,14 @@ def urlopen(url, headers={}, data=None, retries=RETRIES, timeout=TIMEOUT):
return req
except OSError:
logger.error(traceback.format_exc())
-
+
except:
logger.error(traceback.format_exc())
-
+
return None
-def urlopen_without_redirect(url, headers={}, data=None, retries=RETRIES):
+def urlopen_without_redirect(url, headers={}, data=None, retries=RETRIES,
+ timeout=TIMEOUT):
'''请求一个URL, 并返回一个Response对象. 不处理重定向.
使用这个函数可以返回URL重定向(Error 301/302)后的地址, 也可以重到URL中请
@@ -120,7 +121,8 @@ def urlopen_without_redirect(url, headers={}, data=None, retries=RETRIES):
parse_result = urllib.parse.urlparse(url)
for i in range(retries):
try:
- conn = http.client.HTTPConnection(parse_result.netloc)
+ conn = http.client.HTTPConnection(parse_result.netloc,
+ timeout=TIMEOUT)
if data:
conn.request('POST', url, body=data, headers=headers_merged)
else:
@@ -133,7 +135,8 @@ def urlopen_without_redirect(url, headers={}, data=None, retries=RETRIES):
#return None
return None
-def post_multipart(url, headers, fields, files, retries=RETRIES):
+def post_multipart(url, headers, fields, files, retries=RETRIES,
+ timeout=TIMEOUT):
content_type, body = encode_multipart_formdata(fields, files)
schema = urllib.parse.urlparse(url)
@@ -145,7 +148,7 @@ def post_multipart(url, headers, fields, files, retries=RETRIES):
for i in range(retries):
try:
- h = http.client.HTTPConnection(schema.netloc)
+ h = http.client.HTTPConnection(schema.netloc, timeout=timeout)
h.request('POST', url, body=body, headers=headers_merged)
req = h.getresponse()
encoding = req.getheader('Content-encoding')
--
2.9.0