您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用MultipartPostHandler通过Python发布表单数据

使用MultipartPostHandler通过Python发布表单数据

解决此问题的最简单,最兼容的方法似乎是使用“海报”模块。

# test_client.py
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

# Register the streaming http handlers with urllib2
register_openers()

# Start the multipart/form-data encoding of the file "DSC0001.jpg"
# "image1" is the name of the parameter, which is normally set
# via the "name" parameter of the HTML <input> tag.

# headers contains the necessary Content-Type and Content-Length
# datagen is a generator object that yields the encoded parameters
datagen, headers = multipart_encode({"image1": open("DSC0001.jpg")})

# Create the Request object
request = urllib2.Request("http://localhost:5000/upload_image", datagen, headers)
# Actually do the request, and get the response
print urllib2.urlopen(request).read()

这工作得很完美,我也不必为httplib烦恼。该模块位于此处:http ://atlee.ca/software/poster/index.html

python 2022/1/1 18:38:24 有241人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶