旧的twisted版本中,用的是twisted.web.client 中的 getPage(),现在直接废弃了用Agent

@deprecated(Version("Twisted", 16, 7, 0), _GETPAGE_REPLACEMENT_TEXT)
def getPage(url, contextFactory=None, *args, **kwargs):
    """
    Download a web page as a string.

    Download a page. Return a deferred, which will callback with a
    page (as a string) or errback with a description of the error.

    See L{HTTPClientFactory} to see what extra arguments can be passed.
    """
    return _makeGetterFactory(
        url, HTTPClientFactory, contextFactory=contextFactory, *args, **kwargs
    ).deferred

Agent(reactor)其实就是创建http请求代理对象

直接上代码

from twisted.internet import reactor
from twisted.web.client import Agent, readBody

def cbRequest(response):
    print(response)
    print(response.__dir__())
    print(response.version)
    print(response.code)
    print(response.phrase)
    d = readBody(response)
    d.addCallback(cbBody, "helloworld")

def cbBody(body,message):
    #print(body,message)
    pass

agent = Agent(reactor)
d = agent.request(b"GET",b"http://www.baidu.com",None,None)
d.addCallback(cbRequest)
reactor.run()

cbRequest回调拿到的response 是一些http 头部的信息,要想拿到包体还得通过readBody()来获取,readBody返回的是一个deffer,设置一下回调在回调里边就能获取http包体的内容。

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐