【发布时间】:2021-01-04 09:02:58
【问题描述】:
我想使用 Twisted Web Client 发送一个带有多个参数的 POST 请求:
- 图片:图片
- metadata : 带有元数据的 json 文档 我需要使用纯 Twisted,而不需要 Treq 和 requests 等外部库。
目前只能发送一个参数,尝试了几种方法都没有成功。
有人知道如何改变身体来实现这个目标吗?
from __future__ import print_function
from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
from bytesprod import BytesProducer
agent = Agent(reactor)
body = BytesProducer(b"hello, world")
d = agent.request(
b'POST',
b'http://httpbin.org/post',
Headers({'User-Agent': ['Twisted Web Client Example'],
'Content-Type': ['text/x-greeting']}),
body)
def cbResponse(ignored):
print('Response received')
d.addCallback(cbResponse)
def cbShutdown(ignored):
reactor.stop()
d.addBoth(cbShutdown)
reactor.run()
【问题讨论】:
标签: python twisted twisted.web