标签:item oct 文件上传 ica post str pre upload http
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\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 += "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\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写个简单的文件上传是有多难,要么那么复杂,要么各种,,,老子来写个简单的
标签:item oct 文件上传 ica post str pre upload http
原文地址:http://www.cnblogs.com/liguangsunls/p/6918081.html