我需要将文件发送到服务器,该服务器由chunck进行api chunck。我能够做到,但它只适用于大小为1到Mb的文件。如果它更大(甚至2 Mb),它将返回错误:
File "test1.py", line 39, in <module>
conn.send(chunk)
File "/usr/lib/python2.7/httplib.py", line 794, in send
self.sock.sendall(data)
File "/usr/lib/python2.7/ssl.py", line 229, in sendall
v = self.send(data[count:])
File "/usr/lib/python2.7/ssl.py", line 198, in send
v = self._sslobj.write(data)
socket.error: [Errno 32] Broken pipe
这是我的代码:
import httplib
import json
import sys
import os.path
file_name = "some_file"
total_size = os.path.getsize(file_name)
source_file = open(file_name)
path = "/path/param1=value1"
conn = httplib.HTTPSConnection("www.example.org")
conn.connect()
conn.putrequest("POST", path)
conn.putheader("Content-Type", "some content type")
conn.putheader("Content-Length", str(total_size))
conn.endheaders()
while True:
chunk = source_file.read(1024)
if not chunk:
break
conn.send(chunk)
response = conn.getresponse()
data = response.read()
print json.loads(data)
更新:
更准确的错误是:
Unexpected error: [Errno 1] _ssl.c:1246: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry