如何通过python twisted HTTPClient生成POST和GET请求?

问题描述 投票:0回答:1

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

  1. 用户登录时:POST / login?user = bob&pass = abc123 HTTP / 1.1主机:bank.com
  2. 当用户转账时:GET / transfer?to = badguy&amt = 100 HTTP / 1.1主机:bank.com 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)

python http web client twisted
1个回答
0
投票

你想停止使用HTTPClient。相反,使用Agent或第三方treq

GET生成POSTAgent

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生成POSTtreq

import treq
d_get = treq.get(url)
d_post = treq.post(url)
© www.soinside.com 2019 - 2024. All rights reserved.