如何使用python twisted HTTPClient生成POST和GET请求?

2024-04-19 18:53:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个HTTP客户端。这是一个简单的模拟银行网站。它需要能够发送两种请求:

  1. 用户登录时: 发布/登录?用户=bob&pass=abc123 HTTP/1.1
 主持人:银行网在
  2. 当用户转账时: 获取/转移?收件人=坏人金额=100 HTTP/1.1
 主持人:银行网 Cookie:login=fde874

我用python twisted实现,我编写了HTTPClient的一个子类:

class BankClient(HTTPClient):
    def genReq():
       # How to write code to generate and send the Two request?

    def connectionMode(self):
        genReq()



class BankCllientFactory(ClientFactory):
    protocol = BankClient
    def __init__(self):
       self.done = Defered()


def main(reactor):
   factory= BankClientFactory()
   reactor.connectTCP('localhost',8080,factory)
   return factory.done
if __name__ =='__main__':
    task.react(main)


Tags: to用户selfhttpmainfactorydef银行
1条回答
网友
1楼 · 发布于 2024-04-19 18:53:45

您想停止使用HTTPClient。相反,请使用^{}或第三方^{}。在

要生成GET和{},请执行以下操作:

from twisted.web.client import Agent
from twisted.internet import reactor
agent = Agent(reactor)
d_get = agent.request(b"GET", uri)
d_post = agent.request(b"POST", uri)

要生成GET和{},请执行以下操作:

^{pr2}$

相关问题 更多 >