def post_multipart(url, headers, fields, files, retries=RETRIES):
content_type, body = encode_multipart_formdata(fields, files)
schema = urllib.parse.urlparse(url)
headers_merged = default_headers.copy()
for key in headers.keys():
headers_merged[key] = headers[key]
headers_merged['Content-Type'] = content_type
headers_merged['Content-length'] = str(len(body))
for i in range(retries):
try:
h = http.client.HTTPConnection(schema.netloc)
h.request('POST', url, body=body, headers=headers_merged)
req = h.getresponse()
encoding = req.getheader('Content-encoding')
req.data = req.read()
if encoding == 'gzip':
req.data = gzip.decompress(req.data)
elif encoding == 'deflate':
req.data = zlib.decompress(req.data, -zlib.MAX_WBITS)
return req
except OSError:
logger.error(traceback.format_exc())
except:
logger.error(traceback.format_exc())
#return None
return None
评论列表
文章目录