def upload(url,params):
'''
上传文件到server,不适合大文件
@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
Content-Disposition: form-data;"
" name="%s";filename="%s"
Content-Type: application/octet-stream
"
%(BOUNDARY,key.replace("@",""),os.path.basename(value))
fp = open(value,"rb")
data += fp.read()
fp.close()
data +="
"
else:
data += "--%s
Content-Disposition: form-data; name="%s"
%s
"
%(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"})