python http通信问题求助!!!
先上代码
client:
#!/usr/bin/env python
import httplib
fd = open("fsphoto","rb")
fd.seek(0,2)
flen = fd.tell()
print flen
fd.seek(0,0)
conn = httplib.HTTPConnection('localhost',5136)
print "requesting..."
##########################
#conn.connect()
conn.putrequest("UPFILE",'/')
conn.putheader("Length",str(flen))
conn.endheaders()
##########################
rest = flen
while rest >0:
buf = fd.read(1024)
rest -= len(buf)
conn.send(buf)
fd.close()
r1 = conn.getresponse()
#print r1.status,r1.reason
conn.close()
server:
#!/usr/bin/env python
from BaseHTTPServer import HTTPServer,BaseHTTPRequestHandler
import shutil
class MyHttpHandler(BaseHTTPRequestHandler):
protocol_version = 'HTTP/1.1'
def do_UPFILE(self):
print self.path #print the path of client request
flen = self.headers['Length']
print flen
fd = open('../fsphoto','wb')
rest = int(flen)
while rest >0:
data = self.rfile.read(1024)
fd.write(data)
rest -= len(data)
fd.close()
self.send_response(200,'ok')
self.end_headers()
httpd = HTTPServer('localhost',5136,MyHttpHandler)
httpd.serve_forever()
我想作一个用http协议上传文件的东西,故先写了这个小程序测试一下,现在的问题是东西是可以传的,但可能因为socket传送我这里管不了的缘故吧,文件的最后部分client端迟迟不能发送过去,也导致server端出不了while循环,则client因此又没法getresponse,client和server都停在那里,请高人指点一下,这该怎么办,我查了下python的手册,也没找到什么好方法,各位,帮帮忙阿
[解决办法]
最简单的方法,就是抓一下HTTP包, 看看你的HTTP数据包是否是按 HTTP 协议走 的.
哪里出错了就修正那里...
建议你使用 urllib 库来完成这些操作.