Package: aiosocks
Dependenciespython 3.5+
aiohttp 2.3.2+FeaturesSOCKS4, SOCKS4a and SOCKS5 versionProxyConnector for aiohttpSOCKS “CONNECT” commandTODOUDP associateTCP port bindingInstallationYou can install it using Pip:pip install aiosocksIf you want the latest development version, you can install it from source:git clone git@github.com:nibrag/aiosocks.git
cd aiosocks
python setup.py installUsagedirect usageimportasyncioimportaiosocksasyncdefconnect():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()A wrapper for create_connection() returning a (reader, writer) pair# 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')error handlingSocksErroris a base class for:NoAcceptableAuthMethodsLoginAuthenticationFailedInvalidServerVersionInvalidServerReplytry: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 otherortry:transport,protocol=awaitaiosocks.create_connection(lambda:Protocol,proxy=socks5_addr,proxy_auth=socks5_auth,dst=dst)exceptaiosocks.SocksConnectionError:# connection errorexceptaiosocks.SocksError:# socks erroraiohttp usageimportasyncioimportaiohttpimportaiosocksfromaiosocks.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()