def upload(url,params):
'''
上传文件到服务器,不适合大文件
@params url 你懂的
@params {"action":"xxx","@file","file_path"} 普通参数 key:value 文件key头部加@
'''
import os
import urllib2
BOUNDARY = "0450de9528f040078csuxianbaozic06"
data = ""
for key,value in params.items():
if "@" in key:
data += "--%s\r\nContent-Disposition: form-data;" " name=\"%s\";filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n" %(BOUNDARY,key.replace("@",""),os.path.basename(value))
fp = open(value,"rb")
data += fp.read()
fp.close()
data +="\r\n"
else:
data += "--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n" %(BOUNDARY,key,value)
data += "--%s--"%(BOUNDARY,)
request = urllib2.Request(url)
request.add_header("Content-Type","multipart/form-data; boundary="+BOUNDARY)
return urllib2.urlopen(request,data).read()
upload("http://www.baidu.com",{"@file":"/xxx/xxx.jpg"})
python写个简单的文件上传是有多难,要么那么复杂,要么各种,,,老子来写个简单的,布布扣,bubuko.com
python写个简单的文件上传是有多难,要么那么复杂,要么各种,,,老子来写个简单的