尝试使用带有urllib和PySocks的Python中的SOCK4代理打开URL失败
left
有人知道可能是什么问题,为什么会失败?还是有人使用SOCKS4代理在Python中打开URL的另一种方法(尝试使用urllib和PySocks文档示例)?
我检查并且代理已启动,并且应该可以工作(与他人尝试-在使用SOCKS4作为方案时总是失败相同)。使用HTTP作为代理可以很好地工作-urllib.error.URLError: <urlopen error Socket error: 0x5b: Request rejected or failed>
。
proxy = proxies[1]
调试错误对我没有帮助-error.reason.socket_err为:
import socks
import socket
from sockshandler import SocksiPyHandler
import contextlib
from urllib import request
TIMEOUT = 50
if __name__ == '__main__':
headr = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X vAppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
proxies = [("SOCKS4","88.198.224.84", 4145), ("HTTP","159.69.91.65", 3128)]
proxy = proxies[0]
teipe = socks.PROXY_TYPE_SOCKS4
if proxy[0] == "SOCKS4":
teipe = socks.PROXY_TYPE_SOCKS4
elif proxy[0] == "HTTP":
teipe = socks.PROXY_TYPE_HTTP
#socks.setdefaultproxy(teipe, proxy[1], proxy[2])
#socket.socket = socks.socksocket
shandler = SocksiPyHandler(teipe, proxy[1], proxy[2])
baseurl = "https://google.com/"
opener = request.build_opener(shandler)
request.install_opener(opener)
req = request.Request(baseurl, data=None, headers=headr)
with contextlib.closing(request.urlopen(req, timeout=TIMEOUT)) as googl:
print("Opened " + baseurl + " with " + str(request.getproxies()))