使用 urllib2 和 PySocks 的 SOCKS5 代理 [英] SOCKS5 proxy using urllib2 and PySocks

查看:61
本文介绍了使用 urllib2 和 PySocks 的 SOCKS5 代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 urllib2 和 PySocks 连接到 SOCKS5 代理.我的代理有一个用户名和密码,我使用下面的代码,但是我总是得到一个 socks.SOCKS5Error: 0x02: Connection not allowed by ruleset message 当我尝试连接时.有谁知道我做错了什么..?

I'm trying to connect to a SOCKS5 proxy using urllib2 and PySocks. My proxy has a username and password and I use the below code, however I always get a socks.SOCKS5Error: 0x02: Connection not allowed by ruleset message when I'm trying to connect. Would anyone know what I'm doing wrong..?

import socket
import socks
import urllib2

socks.set_default_proxy(socks.SOCKS5, "xx.xx.xx", 8080, 'username','pass')
socket.socket = socks.socksocket


hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
   'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
   'Accept-Encoding': 'none',
   'Accept-Language': 'en-US,en;q=0.8',
   'Connection': 'keep-alive'}

site = 'http://www.google.com'
req = urllib2.Request(site, headers=hdr)
try:
    page = urllib2.urlopen(req)
except urllib2.HTTPError, e:
    print e.fp.read()

content = page.read()
print content

完全错误:socks.SOCKS5Error: 0x02: 规则集不允许连接

Full error: socks.SOCKS5Error: 0x02: Connection not allowed by ruleset

包含新的错误信息

推荐答案

通过传递 'True' 修复

Fixed by passing 'True'

socks.set_default_proxy(socks.SOCKS5, "109.201.154.195", 1080, True,  'user','pass')

这篇关于使用 urllib2 和 PySocks 的 SOCKS5 代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆