本文介绍了httplib2 完全支持 http 代理吗?Socks 代理有效,但 http 无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.我无法让任何 http 代理工作.Socks 代理 (socks4/5) 工作正常.任何想法为什么?urllib2 与代理一起工作得很好.我很困惑.谢谢..

Here is my code. I cannot get any http proxy to work. Socks proxy (socks4/5) works fine though. Any ideas why? urllib2 works fine with proxies though. I am confused. Thanks..

代码:

  1 import socks
  2 import httplib2
  3 import BeautifulSoup
  4
  5 httplib2.debuglevel=4
  6
  7 http = httplib2.Http(proxy_info = httplib2.ProxyInfo(3, '213.30.160.160', 80))
  8
  9 main_url = 'http://cuil.com'
 10
 11 response, content = http.request(main_url, 'GET')
 12
 13 #html_content = BeautifulSoup(content)
 14
 15 print response

输出:

connect: (cuil.com, 80)
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    response, content = http.request(main_url, 'GET')
  File "/home/kk/bin/pythonlib/httplib2/__init__.py", line 1129, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/home/kk/bin/pythonlib/httplib2/__init__.py", line 901, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/home/kk/bin/pythonlib/httplib2/__init__.py", line 862, in _conn_request
    conn.request(method, request_uri, body, headers)
  File "/usr/lib/python2.5/httplib.py", line 866, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.5/httplib.py", line 889, in _send_request
    self.endheaders()
  File "/usr/lib/python2.5/httplib.py", line 860, in endheaders
    self._send_output()
  File "/usr/lib/python2.5/httplib.py", line 732, in _send_output
    self.send(msg)
  File "/usr/lib/python2.5/httplib.py", line 699, in send
    self.connect()
  File "/home/kk/bin/pythonlib/httplib2/__init__.py", line 740, in connect
    self.sock.connect(sa)
  File "/home/kk/bin/pythonlib/socks.py", line 383, in connect
    self.__negotiatehttp(destpair[0],destpair[1])
  File "/home/kk/bin/pythonlib/socks.py", line 349, in __negotiatehttp
    raise HTTPError((statuscode,statusline[2]))
socks.HTTPError: (403, 'Forbidden')

推荐答案

看起来这是 httplib2 的一个未解决的问题:http://code.google.com/p/httplib2/issues/detail?id=38

Looks like this is an open issue with httplib2: http://code.google.com/p/httplib2/issues/detail?id=38

这篇关于httplib2 完全支持 http 代理吗?Socks 代理有效,但 http 无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:45