Python 版本为 3.5 ,在不使用类似Requests等第三方 library 的情况下,怎么发送 post multipart/form-data HTTP 请求(会有文件上传)?
可以的话,麻烦给出完整的代码示例,谢谢。
我找了好久,使用 urllib 来发送,但是都不成功。我找到的资料有:
最后我使用了一个workaround来解决,Python直接调用curl,参考Execute curl command within a Python script - Stack Overflow 的一个答案
其实Python - HTTP multipart/form-data POST request - Stack Overflow这个问题已经给出了答案了,之前是我搞错了,觉得这种方法不行。
1
learningman 2022-12-15 20:34:39 +08:00
你的第二个不就是吗,binascii 是内置库
|
2
ClericPy 2022-12-15 21:01:30 +08:00
data = urlencode(form, doseq=True).encode("utf-8")
headers.setdefault("Content-Type", "application/x-www-form-urlencoded") 最近正在写 built-ins 补充工具, 还没开源, 你先试试吧 |
3
ClericPy 2022-12-15 21:01:58 +08:00
发错了... 忽略我, 没看清楚头...
|
4
haha512 2022-12-15 23:16:40 +08:00
```
import urllib.request import urllib.parse with open('myfile.txt', 'rb') as f: file_data = f.read() # 编码表单数据 form_data = urllib.parse.urlencode({ 'file': file_data, 'field1': 'value1', 'field2': 'value2' }).encode('utf-8') # 发送请求 req = urllib.request.Request('http://httpbin.org/post', data=form_data, headers={ 'Content-Type': 'multipart/form-data' }) res = urllib.request.urlopen(req) # 读取响应 print(res.read().decode('utf-8')) ``` |
5
JasonLaw OP |
7
haha512 2022-12-16 10:57:45 +08:00 1
这个实测可以
` import mimetypes, http.client import json boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T' # Randomly generated fileName='./1.txt' dataList=[] # Add boundary and header dataList.append('--' + boundary) dataList.append('Content-Disposition: form-data; name={0}; filename={0}'.format(fileName)) fileType = mimetypes.guess_type(fileName)[0] or 'application/octet-stream' dataList.append('Content-Type: {}'.format(fileType)) dataList.append('') with open(fileName) as f: dataList.append(f.read()) dataList.append('--'+boundary+'--') dataList.append('') contentType = 'multipart/form-data; boundary={}'.format(boundary) body = '\r\n'.join(dataList) headers = {'Content-type': contentType} conn = http.client.HTTPConnection('httpbin.org:80') req = conn.request('POST', '/post', body, headers) print(str(conn.getresponse().read().decode('utf-8'))) ` 返回响应 ` { "args": {}, "data": "", "files": { "./1.txt": "asdgsadga" }, "form": {}, "headers": { "Accept-Encoding": "identity", "Content-Length": "173", "Content-Type": "multipart/form-data; boundary=wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T", "Host": "httpbin.org", "X-Amzn-Trace-Id": "Root=1-639bde59-2e7b61940f7df7794549546b" }, "json": null, "origin": "123.234.99.81", "url": "http://httpbin.org/post" } ` |
9
JasonLaw OP @haha512 #7 谢谢,https://stackoverflow.com/questions/15767785/python-http-multipart-form-data-post-request 已经给出了答案,是我自己搞错了。
|