zoukankan      html  css  js  c++  java
  • python3下的twistedPOST请求网页

    在python2中我们使用twisted比较方便,网上资料也比较多,但是通常在python3中使用的时候,并不能成功。我也是找了好多资料没有成功之后,自己去尝试做小白鼠,测试了很久之后,发现传递给twisted的所有数据都需要是bytes类型的。直接看代码吧(亲测可用):

    from twisted.internet import reactor
    from twisted.web.client import getPage
    import urllib.parse
    
    num = 0
    
    a = []
    
    
    def one_done(arg):
        global num
        print(type(arg))
        print(arg.decode())
        a.append(arg)
        num += 1
        if num == 3:
            reactor.stop()
    
    
    cookies = {
        b'123': b'654'
    }
    post_data = urllib.parse.urlencode({'check_data': 'adf'})
    post_data = bytes(post_data, encoding='utf8')
    headers = {b'Content-Type': b'application/x-www-form-urlencoded'}
    for i in range(3):
        response = getPage(bytes('http://dig.chouti.com/login', encoding='utf8'),
                           method=bytes('POST', encoding='utf8'),
                           postdata=post_data,
                           headers=headers,
                           cookies=cookies)
        response.addBoth(one_done)
    
    reactor.run()
    
    print(a)
    要注意的是,postdata这个字典是直接转换为字符串然后转换为bytes,headers和cookies只是将键和值转换为bytes类型了。
    

    注意:postdata这个字典是直接转换为字符串然后转换为bytes,headers和cookies只是将键和值转换为bytes类型了。

  • 相关阅读:
    Go语言http之请求接收和处理 代码
    C++之IO流的状态以及使用
    C++之指向函数的指针
    C++之数组类型的形参
    C++之vector类型的形参
    C++之形参
    C++之运算符
    C++之多维数组
    C++之动态数组
    C++之指针
  • 原文地址:https://www.cnblogs.com/sxzwj/p/6846486.html
Copyright © 2011-2022 走看看