Package: aiosocks2
Dependenciespython 3.5+
aiohttp 2.3.2+FeaturesFixed original library to use in 2021 onwardsSOCKS4, SOCKS4a and SOCKS5 versionProxyConnector for aiohttpSOCKS “CONNECT” commandTODOUDP associateTCP port bindingInstallationYou can install it using Pip:pip install aiosocks2If you want the latest development version, you can install it from source:git clone git@github.com:ultrafunkamsterdam/aiosocks2.git
cd aiosocks2
python setup.py installUsagedirect usageimportasyncioimportaiosocks2asyncdefconnect():socks5_addr=aiosocks2.Socks5Addr('127.0.0.1',1080)socks4_addr=aiosocks2.Socks4Addr('127.0.0.1',1080)socks5_auth=aiosocks2.Socks5Auth('login','pwd')socks4_auth=aiosocks2.Socks4Auth('ident')dst=('github.com',80)# socks5 connecttransport,protocol=awaitaiosocks2.create_connection(lambda:Protocol,proxy=socks5_addr,proxy_auth=socks5_auth,dst=dst)# socks4 connecttransport,protocol=awaitaiosocks2.create_connection(lambda:Protocol,proxy=socks4_addr,proxy_auth=socks4_auth,dst=dst)# socks4 without auth and local domain name resolvingtransport,protocol=awaitaiosocks2.create_connection(lambda:Protocol,proxy=socks4_addr,proxy_auth=None,dst=dst,remote_resolve=False)# use socks protocoltransport,protocol=awaitaiosocks2.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=awaitaiosocks2.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=awaitaiosocks2.create_connection(lambda:Protocol,proxy=socks5_addr,proxy_auth=socks5_auth,dst=dst)exceptaiosocks2.SocksConnectionError:# connection errorexceptaiosocks2.LoginAuthenticationFailed:# auth failedexceptaiosocks2.NoAcceptableAuthMethods:# All offered SOCKS5 authentication methods were rejectedexcept(aiosocks2.InvalidServerVersion,aiosocks2.InvalidServerReply):# something wrongexceptaiosocks2.SocksError:# something otherortry:transport,protocol=awaitaiosocks2.create_connection(lambda:Protocol,proxy=socks5_addr,proxy_auth=socks5_auth,dst=dst)exceptaiosocks2.SocksConnectionError:# connection errorexceptaiosocks2.SocksError:# socks erroraiohttp usageimportasyncioimportaiohttpimportaiosocks2fromaiosocks2.connectorimportProxyConnector,ProxyClientRequestasyncdefload_github_main():auth5=aiosocks2.Socks5Auth('proxyuser1',password='pwd')auth4=aiosocks2.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, etcexceptaiosocks2.SocksError:# communication problemif__name__=='__main__':loop=asyncio.get_event_loop()loop.run_until_complete(load_github_main())loop.close()