我正在尝试使用以下代码上传文件:
url = "/folder/sub/interface?"
connection = httplib.HTTPConnection('www.mydomain.com')
def sendUpload(self):
fields = []
file1 = ['file1', '/home/me/Desktop/sometextfile.txt']
f = open(file1[1], 'r')
file1.append(f.read())
files = [file1]
content_type, body = self.encode_multipart_formdata(fields, files)
myheaders['content-type'] = content_type
myheaders['content-length'] = str(len(body))
upload_data = urllib.urlencode({'command':'upload'})
self.connection.request("POST", self.url + upload_data, {}, myheaders)
response = self.connection.getresponse()
if response.status == 200:
data = response.read()
self.connection.close()
print data
encode_multipart_formdata() 来自http://code.activestate.com/recipes/146306/
当我执行该方法时,需要很长时间才能完成。事实上,我不认为它会结束..在网络监视器上我看到数据已传输,但方法并没有结束......
这是为什么?我应该在某处设置超时吗?