在 Twisted 客户端中生成标头
我正在使用 Twisted Web“HTTPClient”协议与 http 服务器对话(使用 Twisted 应用程序作为客户端),问题是我的大多数请求不接受我的标头。是否有某种方法(也许使用某些库)来生成标头? 我将发布一些代码,以便您可以查看
class Local(HTTPClient):
def generateHeaders(self, type=None):
self.sendHeader('User-Agent', 'Twisted Web Client')
if type:
self.sendHeader('Content-Type', 'x-www-form-urlencoded')
self.sendHeader('Accept', '*/*')
self.sendHeader('Host', 'localhost')
self.endHeaders()
def connectionMade(self):
## GENERATE ID
self.userid= self.generateID()
self.sendCommand('POST', '/chat/action=join&user=pirosb3')
self.generateHeaders(type=True)
def dataReceived(self, data):
print data
如您所见,我创建了一个为我生成标头的方法,但我必须说效率不是很高,我总是收到错误响应。
有人知道解决办法吗?
谢谢
i'm using the Twisted web "HTTPClient" protocol to dialogue with a http server (using twisted app as a client), the problem is that most of my request do not accept my header. Is there some sort of way (maybe using certain libraries) to generate header??
I'll post some code so you can have a look
class Local(HTTPClient):
def generateHeaders(self, type=None):
self.sendHeader('User-Agent', 'Twisted Web Client')
if type:
self.sendHeader('Content-Type', 'x-www-form-urlencoded')
self.sendHeader('Accept', '*/*')
self.sendHeader('Host', 'localhost')
self.endHeaders()
def connectionMade(self):
## GENERATE ID
self.userid= self.generateID()
self.sendCommand('POST', '/chat/action=join&user=pirosb3')
self.generateHeaders(type=True)
def dataReceived(self, data):
print data
As you can see, i created a method that generates header for me, but not very efficient i must say, i always get error responses.
Anyone know a solution?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我绝对推荐您使用 Twisted 的
Agent
类以及twisted.web.http_headers
模块中的Header
类。此处提供了一个很好的指南。
如果您需要一个现实生活中的 Twisted HTTP 客户端示例,该客户端向请求添加一些标头并可以读取答案的标头,这里是:
I would definitely recommend you to use Twisted's
Agent
class along with theHeader
class in thetwisted.web.http_headers
module.There is a nice guide available here.
If you need a real life example of a Twisted HTTP client which adds some headers to the request and can read the ones of the answer, here it is: