如何在 Python 中模拟 post 表单来上传文件
展开全部
发个以前用urllib2模块来做的
class HTTPError(urllib2.HTTPDefaultErrorHandler):
def __init__(self):
self.errMsg = ''
def getErrorMsg(self):
return self.errMsg
def http_error_default(self, req, fp, code, msg, hdrs):
if code >= 400 :
self.errMsg = fp.read() 裂简
raise urllib2.HTTPError(req.get_full_url(), code, msg, hdrs, fp)
'''
urllib2 post request: eg. uploadfile
'''
def sendMultipartPost(url, params, files):
#params like this :{'type':'upload','id':'xxxx'}
#files like this: {'file':'C:/xxxx.txt'}
posterParams = []
for key in params:
value = params[key]
try:
posterParams.append(poster.encode.MultipartParam(key, value))
except Exception, e:
print e, key, value
raise e
for key in files:
value = files[key]
try:
value = value.encode(sys.getfilesystemencoding())
答源基 posterParams.append(poster.encode.MultipartParam.from_file(key, value))
except Exception, e:
清谨 print e, key, value
raise e
try:
datagen, headers = poster.encode.multipart_encode(posterParams)
except Exception, e:
print e, key, value
raise e
if headers is None:
headers = {}
try:
request = urllib2.Request(url, datagen, headers)
request.add_header('Accept-encoding', 'gzip')
request.add_header("Accept", "*/*")
# print request
# print request.get_data()
opener,err = getUrllib2(True, False)
response = opener.open(request)
except Exception, e:
print e, url, files
print err.getErrorMsg()
raise e
data = response.read()
'''
data = response.read(16*1024)
length = len(data)
_data = None
while length:
if _data: data += _data
_data = response.read(16*1024)
length = len(_data)
'''
if 'gzip' == response.headers.get('content-encoding', ''):
compressedstream = StringIO.StringIO(data)
gzipper = gzip.GzipFile(fileobj=compressedstream)
data =gzipper.read()
return data
def getUrllib2(upload = False, redirect = False):
if upload:
handlers = poster.streaminghttp.get_handlers()
else:
handlers = []
err = HTTPError()
handlers.append(err)
handlers.append(getCookie())
try:
opener = urllib2.build_opener(*handlers)
except Exception, e:
print err.getErrorMsg()
raise e
return opener,err
def getCookie():
global _cookieProcessor
cookiefile = "./cookies.txt"
try:
httpcookie = cookielib.MozillaCookieJar(cookiefile)
httpcookie.load(ignore_discard=True, ignore_expires=True)
httpcookie = urllib2.HTTPCookieProcessor(httpcookie)
except Exception, e:
print e
httpcookie = _cookieProcessor
# _cookieProcessor = urllib2.HTTPCookieProcessor(cookielib.CookieJar())
return httpcookie
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询