python中socks包(SocksiPy)的安装、使用方法简介 - Go语言中文社区

python中socks包(SocksiPy)的安装、使用方法简介


1、下载SocksiPy

csdn地址:点击打开https://download.csdn.net/download/qq_27378621/10536690

官方地址:点击打开https://sourceforge.net/projects/socksipy/?source=typ_redirect

2、官方文档介绍

地址:点击打开http://socksipy.sourceforge.net/

文件如下:

SocksiPy - A Python SOCKS client module

 

This module was designed to allow developers of Python software that uses the Internet or another TCP/IP-based network to add support for connection through a SOCKS proxy server with as much ease as possible.

 

Usage:

 

Using SocksiPy to establish a connection thorough a proxy takes exactly 4 lines.
Here is an example:

>>> import socks
>>> s = socks.socksocket()
>>> s.setproxy(socks.PROXY_TYPE_SOCKS5,"socks.example.com")
>>> s.connect(("www.example.com",80))
>>> 

For more detailed information, the README file is available.

 

Compatibilty:

 

SocksiPy has been programmed using Python 2.3 on a Windows 2000 machine.
However as it is made purely of Python code, it should run on any OS. Other (both older and newer) versions of Python should work as well.

 

License:

 

SocksiPy is released under a BSD style license. This means than you are allowed to do anything with the software, including redistribution and modification, just don't take credit as if you wrote it. The full license can also be read.

 

Beta note:

 

Please note that SocksiPy is still at an early beta stage. I have tested this module before I have released it, but naturally problems may still occur.
In any case, just don't be surprised if something doesn't work as expected.

 

Download:

 

SocksiPy may be downloaded from the sourceforge project page.

 

The Author:

 

This software has been developed by Dan Haim.

 

The author may be contacted at negativeiq@users.sourceforge.net.

 

3、将下载好的文件中的socks.py 文件复制到python的安装目录下的lib文件夹

下面是我python的lib文件夹路径:

s

在程序中import socks 即可使用该包。

注:该包不支持直接在命令行使用pip方法安装。

 

4、使用sockets全局代理及解除代理

需要用到的函数(类)的部分源码(详见socks.py文件):

def setdefaultproxy(proxytype=None,addr=None,port=None,rdns=True,username=None,password=None):
	"""setdefaultproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
	Sets a default proxy which all further socksocket objects will use,
	unless explicitly changed.
	"""
	global _defaultproxy
	_defaultproxy = (proxytype,addr,port,rdns,username,password)
	
class socksocket(socket.socket):
	"""socksocket([family[, type[, proto]]]) -> socket object
	
	Open a SOCKS enabled socket. The parameters are the same as
	those of the standard socket init. In order for SOCKS to work,
	you must specify family=AF_INET, type=SOCK_STREAM and proto=0.
	"""
	
	def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, _sock=None):
		_orgsocket.__init__(self,family,type,proto,_sock)
		if _defaultproxy != None:
			self.__proxy = _defaultproxy
		else:
			self.__proxy = (None, None, None, None, None, None)
		self.__proxysockname = None
		self.__proxypeername = None
	

设置代理及解除代理:

# 设置全局代理
socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, 代理ip, 代理端口号)
socket.socket = socks.socksocket

# 解除代理
socks.setdefaultproxy()
socket.socket = socks.socksocket

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_27378621/article/details/81012561
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-28 01:46:05
  • 阅读 ( 1479 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢