asyncio和aiohttp的socks代理客户端
aiosocks的Python项目详细描述
依赖关系
Python3.5+ aiohttp 2.3.2+
功能
- socks4、socks4a和socks5版本
- 用于AIOHTTP的ProxyConnector
- socks“connect”命令
待办事项
- udp关联
- TCP端口绑定
安装
您可以使用pip安装它:
pip install aiosocks
如果您需要最新的开发版本,可以从源代码安装它:
git clone git@github.com:nibrag/aiosocks.git cd aiosocks python setup.py install
用法
直接使用
importasyncioimportaiosocksasyncdefconnect():socks5_addr=aiosocks.Socks5Addr('127.0.0.1',1080)socks4_addr=aiosocks.Socks4Addr('127.0.0.1',1080)socks5_auth=aiosocks.Socks5Auth('login','pwd')socks4_auth=aiosocks.Socks4Auth('ident')dst=('github.com',80)# socks5 connecttransport,protocol=awaitaiosocks.create_connection(lambda:Protocol,proxy=socks5_addr,proxy_auth=socks5_auth,dst=dst)# socks4 connecttransport,protocol=awaitaiosocks.create_connection(lambda:Protocol,proxy=socks4_addr,proxy_auth=socks4_auth,dst=dst)# socks4 without auth and local domain name resolvingtransport,protocol=awaitaiosocks.create_connection(lambda:Protocol,proxy=socks4_addr,proxy_auth=None,dst=dst,remote_resolve=False)# use socks protocoltransport,protocol=awaitaiosocks.create_connection(None,proxy=socks4_addr,proxy_auth=None,dst=dst)if__name__=='__main__':loop=asyncio.get_event_loop()loop.run_until_complete(connect())loop.close()
create_connection()的包装器,返回(读写器)对
# StreamReader, StreamWriterreader,writer=awaitaiosocks.open_connection(proxy=socks5_addr,proxy_auth=socks5_auth,dst=dst,remote_resolve=True)data=awaitreader.read(10)writer.write('data')
错误处理
- sockserror是以下项的基类:
- 无访问权限方法
- 登录身份验证失败
- 无效服务器版本
- 无效服务器回复
try:transport,protocol=awaitaiosocks.create_connection(lambda:Protocol,proxy=socks5_addr,proxy_auth=socks5_auth,dst=dst)exceptaiosocks.SocksConnectionError:# connection errorexceptaiosocks.LoginAuthenticationFailed:# auth failedexceptaiosocks.NoAcceptableAuthMethods:# All offered SOCKS5 authentication methods were rejectedexcept(aiosocks.InvalidServerVersion,aiosocks.InvalidServerReply):# something wrongexceptaiosocks.SocksError:# something other
或
try:transport,protocol=awaitaiosocks.create_connection(lambda:Protocol,proxy=socks5_addr,proxy_auth=socks5_auth,dst=dst)exceptaiosocks.SocksConnectionError:# connection errorexceptaiosocks.SocksError:# socks error
aiohttp用法
importasyncioimportaiohttpimportaiosocksfromaiosocks.connectorimportProxyConnector,ProxyClientRequestasyncdefload_github_main():auth5=aiosocks.Socks5Auth('proxyuser1',password='pwd')auth4=aiosocks.Socks4Auth('proxyuser1')ba=aiohttp.BasicAuth('login')# remote resolveconn=ProxyConnector(remote_resolve=True)# or locale resolveconn=ProxyConnector(remote_resolve=False)try:withaiohttp.ClientSession(connector=conn,request_class=ProxyClientRequest)assession:# socks5 proxyasyncwithsession.get('http://github.com/',proxy='socks5://127.0.0.1:1080',proxy_auth=auth5)asresp:ifresp.status==200:print(awaitresp.text())# socks4 proxyasyncwithsession.get('http://github.com/',proxy='socks4://127.0.0.1:1081',proxy_auth=auth4)asresp:ifresp.status==200:print(awaitresp.text())# http proxyasyncwithsession.get('http://github.com/',proxy='http://127.0.0.1:8080',proxy_auth=ba)asresp:ifresp.status==200:print(awaitresp.text())exceptaiohttp.ClientProxyConnectionError:# connection problemexceptaiohttp.ClientConnectorError:# ssl error, certificate error, etcexceptaiosocks.SocksError:# communication problemif__name__=='__main__':loop=asyncio.get_event_loop()loop.run_until_complete(load_github_main())loop.close()