【问题标题】:Why Pycurl returns FAILED为什么 Pycurl 返回 FAILED
【发布时间】:2015-02-25 13:49:58
【问题描述】:

我正在 Python 上编写一个应用程序,它通过各种代理发送发布请求。我为此使用 PycURL 库。 在我的 Linux 机器上,同样的代码运行良好。但在 Windows 响应正文中有时只包含“FAILED”而不是任何 HTTP 代码或连接失败。我在 Linux 上没有看到,所以我真的很困惑,我已经花了一天时间试图找出问题所在。有人可以帮我吗?

这是我的代码:

while (success_request != True) and (self.is_need_to_stop() == False):
    current_proxy = self.get_random_proxy() 
    post_data = {'data' : data }
    request_url = 'http://www.example.com'

    _stringio = StringIO()
    _curl = pycurl.Curl()
    _curl.setopt(pycurl.URL, request_url)
    _curl.setopt(pycurl.WRITEFUNCTION, _stringio.write)
    _curl.setopt(pycurl.POSTFIELDS, urlencode(post_data))
    _curl.setopt(pycurl.PROXY, current_proxy['address'])
    _curl.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_HTTP)
    _curl.setopt(pycurl.USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7')
    _curl.setopt(pycurl.CONNECTTIMEOUT, 15)
    _curl.setopt(pycurl.TIMEOUT, 15)
    _curl.setopt(pycurl.VERBOSE, True)             

    try:
        _curl.perform()
    except Exception:
        print "Proxy: '{0}' is not working!".format(current_proxy['address'])
        success_request = False
        self.del_bad_proxy(current_proxy['index'])
        continue

    _curl.close()

    if self.check_responce(_stringio.getvalue().decode('utf-8')) == False:
        print _stringio.getvalue().decode('utf-8')
        print "Proxy: '{0}' returns 'bad' response!".format(current_proxy['address'])
        self.del_bad_proxy(current_proxy['index'])
        continue
    else:
        success_request = True
        self.proxy_list[current_proxy['index']]['requests_number'] += 1
        self.comments_number += 1
        self.window.comment_posted(self.comments_number)

这是问题输出:

* About to connect() to proxy 85.15.234.1 port 8081 (#0)
*   Trying 85.15.234.1... * connected
* Connected to 85.15.234.1 (85.15.234.1) port 8081 (#0)
> POST http://www.example.com HTTP/1.1    
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7    
Host: http://www.example.com    
Accept: */*    
Proxy-Connection: Keep-Alive    
Content-Length: 42    
Content-Type: application/x-www-form-urlencoded    

< HTTP/1.1 200 OK   
< Server: nginx  
< Date: Mon, 29 Dec 2014 11:00:55 GMT   
< Content-Type: text/html  
< P3P: policyref="/p3p.xml", CP="NOI CURa ADMa DEVa TAIa OUR BUS IND UNI COM NAV INT" 
< Vary: Accept-Encoding    
< Content-Length: 6   
< 

* Connection #0 to host 85.15.234.1 left intact
* Closing connection #0
FAILED
Proxy: '85.15.234.1:8081' returns 'bad' response!
* timeout on name lookup is not supported

【问题讨论】:

  • 如果你通过其他方式访问同一个代理会发生什么:IE、命令行curlrequests模块?
  • @J.F.塞巴斯蒂安 还没试过,会写在这里。

标签: python windows curl proxy pycurl


【解决方案1】:

好吧,事实证明,Windows 中的 pycurl 和 Linux 中的 pycurl 只是在内部有所不同。所以,我使用 requests 库重写了我的代码,现在它可以完美运行了。

【讨论】:

    猜你喜欢
    • 2013-05-18
    • 2013-11-05
    • 2020-08-31
    • 2023-02-10
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多