我如何使用urllib2的SOCKS 4/5代理?

时间:2021-02-18 20:12:19

How can I use a SOCKS 4/5 proxy with urllib2 to download a web page?

如何使用SOCKS 4/5代理和urllib2下载网页?

3 个解决方案

#1


65  

You can use SocksiPy module. Simply copy the file "socks.py" to your Python's lib/site-packages directory, and you're ready to go.

您可以使用SocksiPy模块。只需复制文件“socks”。到Python的lib/site-packages目录,然后就可以开始了。

You must use socks before urllib2. (Try it pip install PySocks )

在urllib2之前,你必须穿短袜。(尝试安装PySocks)

For example:

例如:

import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 8080)
socket.socket = socks.socksocket
import urllib2
print urllib2.urlopen('http://www.google.com').read()

You can also try pycurl lib and tsocks, for more detail, click on here.

您也可以尝试pycurl lib和tsocks,要了解更多细节,请单击这里。

#2


20  

Adding an alternative to pan's answer when you need to use many different proxies at the same time.

当您需要同时使用许多不同的代理时,添加pan的替代答案。

In that case you need to create an opener like you do with a http proxy. There is a code available in GitHub https://gist.github.com/869791

在这种情况下,您需要像使用http代理那样创建一个打开器。GitHub上有一个可用的代码:https://gist.github.com/869791

opener = urllib2.build_opener(SocksiPyHandler(socks.PROXY_TYPE_SOCKS4, 'localhost', 9999))
print opener.open('http://www.whatismyip.com/automation/n09230945.asp').read()

#3


4  

Since SOCKS is a socket level proxy, you have to replace the socket object used by urllib2. Please take a look a this solution. If monkey patching is not good enough for you, then you can try to subclass or copy-modify the code from the urllib2 standard library.

由于SOCKS是套接字级别的代理,所以必须替换urllib2使用的套接字对象。请看看这个解决方案。如果monkey patching对您来说不够好,那么您可以尝试从urllib2标准库对代码进行子类化或复制-修改。

#1


65  

You can use SocksiPy module. Simply copy the file "socks.py" to your Python's lib/site-packages directory, and you're ready to go.

您可以使用SocksiPy模块。只需复制文件“socks”。到Python的lib/site-packages目录,然后就可以开始了。

You must use socks before urllib2. (Try it pip install PySocks )

在urllib2之前,你必须穿短袜。(尝试安装PySocks)

For example:

例如:

import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 8080)
socket.socket = socks.socksocket
import urllib2
print urllib2.urlopen('http://www.google.com').read()

You can also try pycurl lib and tsocks, for more detail, click on here.

您也可以尝试pycurl lib和tsocks,要了解更多细节,请单击这里。

#2


20  

Adding an alternative to pan's answer when you need to use many different proxies at the same time.

当您需要同时使用许多不同的代理时,添加pan的替代答案。

In that case you need to create an opener like you do with a http proxy. There is a code available in GitHub https://gist.github.com/869791

在这种情况下,您需要像使用http代理那样创建一个打开器。GitHub上有一个可用的代码:https://gist.github.com/869791

opener = urllib2.build_opener(SocksiPyHandler(socks.PROXY_TYPE_SOCKS4, 'localhost', 9999))
print opener.open('http://www.whatismyip.com/automation/n09230945.asp').read()

#3


4  

Since SOCKS is a socket level proxy, you have to replace the socket object used by urllib2. Please take a look a this solution. If monkey patching is not good enough for you, then you can try to subclass or copy-modify the code from the urllib2 standard library.

由于SOCKS是套接字级别的代理,所以必须替换urllib2使用的套接字对象。请看看这个解决方案。如果monkey patching对您来说不够好,那么您可以尝试从urllib2标准库对代码进行子类化或复制-修改。