![alt text][pypi_version] ![alt text][licence_version]
# Proxy Providers for Web3Py
A library for connecting to Web3 RPC providers using a proxy
Tested with:
* Python 3.6+
Supports Proxy usage for:
* Http RPC (Sync, Async)
* Websocket RPC (Sync, Async)
* Websocket RPC with subscription (Sync, Async)
Use the following command to install using pip:
```
pip install web3-proxy-providers
```
To use the providers, you need web3 with version above 6.0.0, current version is 6.0.0b9 (beta)
```
pip install web3==6.0.09b
```
## Usage example
### Http Provider with Proxy
Use `HttpWithProxyProvider` class which supports http and socks proxy
```python
from web3 import Web3
from web3_proxy_providers import HttpWithProxyProvider
provider = HttpWithProxyProvider(
endpoint_uri='https://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',
proxy_url='socks5h://localhost:1080'
)
web3 = Web3(
provider=provider,
)
print(web3.eth.block_number)
```
### Async Http Provider with Proxy
Use `AsyncHTTPWithProxyProvider` class to connect to an RPC with asyncio using a proxy. both http proxy and socks proxy are supported
```python
import asyncio
from web3 import Web3
from web3.eth import AsyncEth
from python_socks import ProxyType
from web3_proxy_providers import AsyncHTTPWithProxyProvider
async def main():
provider = AsyncHTTPWithProxyProvider(
proxy_type=ProxyType.SOCKS5,
proxy_host='localhost',
proxy_port=1080,
endpoint_uri='https://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',
)
web3 = Web3(
provider=provider,
modules={'eth': (AsyncEth,)},
)
print(await web3.eth.block_number)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```
### Async Websocket Provider with Proxy
Use `AsyncWebsocketWithProxyProvider` class to connect to a websocket RPC with asyncio using a proxy. both http proxy and socks proxy are supported
```python
import asyncio
from web3 import Web3
from web3.eth import AsyncEth
from python_socks import ProxyType
from web3_proxy_providers import AsyncWebsocketWithProxyProvider
async def main(loop: asyncio.AbstractEventLoop):
provider = AsyncWebsocketWithProxyProvider(
loop=loop,
proxy_type=ProxyType.SOCKS5,
proxy_host='localhost',
proxy_port=1080,
endpoint_uri='wss://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',
)
web3 = Web3(
provider=provider,
modules={'eth': (AsyncEth,)},
)
print(await web3.eth.block_number)
if __name__ == '__main__':
async_loop = asyncio.get_event_loop()
async_loop.run_until_complete(main(loop=async_loop))
```
### Async Websocket Provider with Proxy with Subscription support
Use `AsyncSubscriptionWebsocketWithProxyProvider` class to connect to a websocket RPC with asyncio using a proxy. both http proxy and socks proxy are supported
Learn more about realtime events and eth_subscribe:
* [Ethereum/Geth Docs](https://geth.ethereum.org/docs/interacting-with-geth/rpc/pubsub)
* [Alchemy Docs](https://docs.alchemy.com/reference/eth-subscribe-polygon)
```python
import asyncio
from Crypto.Hash import keccak
from web3 import Web3
from python_socks import ProxyType
from web3_proxy_providers import AsyncSubscriptionWebsocketWithProxyProvider
async def callback(subs_id: str, json_result):
print(json_result)
async def main(loop: asyncio.AbstractEventLoop):
provider = AsyncSubscriptionWebsocketWithProxyProvider(
loop=loop,
proxy_type=ProxyType.SOCKS5,
proxy_host='localhost',
proxy_port=1080,
endpoint_uri='wss://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',
)
# subscribe to Deposit and Withdrawal events for WETH contract
weth_contract_address = Web3.to_checksum_address('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2')
deposit_topic = "0x" + keccak.new(data=b'Deposit(address,uint256)', digest_bits=256).hexdigest()
withdrawal_topic = "0x" + keccak.new(data=b'Withdrawal(address,uint256)', digest_bits=256).hexdigest()
subscription_id = await provider.subscribe(
[
'logs',
{
"address": weth_contract_address,
"topics": [deposit_topic, withdrawal_topic]
}
],
callback
)
print(f'Subscribed with id {subscription_id}')
# unsubscribe after 30 seconds
await asyncio.sleep(30)
await provider.unsubscribe(subscription_id)
if __name__ == '__main__':
async_loop = asyncio.get_event_loop()
async_loop.run_until_complete(main(loop=async_loop))
```
[pypi_version]: https://img.shields.io/pypi/v/web3-proxy-providers.svg "PYPI version"
[licence_version]: https://img.shields.io/badge/license-MIT%20v2-brightgreen.svg "MIT Licence"
Raw data
{
"_id": null,
"home_page": "https://github.com/sinarezaei/web3_proxy_providers",
"name": "web3-proxy-providers",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "",
"author": "Sina Rezaei",
"author_email": "sinarezaei1991@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/27/33/d584c4dcaa70fd0100d476a45b5cccd4d8121e680b76065701bd94889bdb/web3-proxy-providers-0.3.tar.gz",
"platform": null,
"description": "![alt text][pypi_version] ![alt text][licence_version]\n\n# Proxy Providers for Web3Py\n\nA library for connecting to Web3 RPC providers using a proxy\n\nTested with:\n* Python 3.6+\n\nSupports Proxy usage for:\n* Http RPC (Sync, Async)\n* Websocket RPC (Sync, Async)\n* Websocket RPC with subscription (Sync, Async)\n\nUse the following command to install using pip:\n```\npip install web3-proxy-providers\n```\n\nTo use the providers, you need web3 with version above 6.0.0, current version is 6.0.0b9 (beta)\n\n```\npip install web3==6.0.09b\n```\n\n## Usage example\n### Http Provider with Proxy\nUse `HttpWithProxyProvider` class which supports http and socks proxy\n\n```python\nfrom web3 import Web3\nfrom web3_proxy_providers import HttpWithProxyProvider\n\nprovider = HttpWithProxyProvider(\n endpoint_uri='https://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',\n proxy_url='socks5h://localhost:1080'\n)\nweb3 = Web3(\n provider=provider,\n)\nprint(web3.eth.block_number)\n```\n\n### Async Http Provider with Proxy\nUse `AsyncHTTPWithProxyProvider` class to connect to an RPC with asyncio using a proxy. both http proxy and socks proxy are supported\n\n```python\nimport asyncio\nfrom web3 import Web3\nfrom web3.eth import AsyncEth\nfrom python_socks import ProxyType\nfrom web3_proxy_providers import AsyncHTTPWithProxyProvider\n\nasync def main():\n provider = AsyncHTTPWithProxyProvider(\n proxy_type=ProxyType.SOCKS5,\n proxy_host='localhost',\n proxy_port=1080,\n endpoint_uri='https://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',\n )\n web3 = Web3(\n provider=provider,\n modules={'eth': (AsyncEth,)},\n )\n print(await web3.eth.block_number)\n\nif __name__ == '__main__':\n loop = asyncio.get_event_loop()\n loop.run_until_complete(main())\n```\n\n### Async Websocket Provider with Proxy\nUse `AsyncWebsocketWithProxyProvider` class to connect to a websocket RPC with asyncio using a proxy. both http proxy and socks proxy are supported\n\n```python\nimport asyncio\nfrom web3 import Web3\nfrom web3.eth import AsyncEth\nfrom python_socks import ProxyType\nfrom web3_proxy_providers import AsyncWebsocketWithProxyProvider\n\nasync def main(loop: asyncio.AbstractEventLoop):\n provider = AsyncWebsocketWithProxyProvider(\n loop=loop,\n proxy_type=ProxyType.SOCKS5,\n proxy_host='localhost',\n proxy_port=1080,\n endpoint_uri='wss://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',\n )\n web3 = Web3(\n provider=provider,\n modules={'eth': (AsyncEth,)},\n )\n print(await web3.eth.block_number)\n\nif __name__ == '__main__':\n async_loop = asyncio.get_event_loop()\n async_loop.run_until_complete(main(loop=async_loop))\n```\n\n### Async Websocket Provider with Proxy with Subscription support\nUse `AsyncSubscriptionWebsocketWithProxyProvider` class to connect to a websocket RPC with asyncio using a proxy. both http proxy and socks proxy are supported\n\nLearn more about realtime events and eth_subscribe:\n* [Ethereum/Geth Docs](https://geth.ethereum.org/docs/interacting-with-geth/rpc/pubsub)\n* [Alchemy Docs](https://docs.alchemy.com/reference/eth-subscribe-polygon)\n\n\n```python\nimport asyncio\nfrom Crypto.Hash import keccak\nfrom web3 import Web3\nfrom python_socks import ProxyType\nfrom web3_proxy_providers import AsyncSubscriptionWebsocketWithProxyProvider\n\nasync def callback(subs_id: str, json_result):\n print(json_result)\n\nasync def main(loop: asyncio.AbstractEventLoop):\n provider = AsyncSubscriptionWebsocketWithProxyProvider(\n loop=loop,\n proxy_type=ProxyType.SOCKS5,\n proxy_host='localhost',\n proxy_port=1080,\n endpoint_uri='wss://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',\n )\n \n # subscribe to Deposit and Withdrawal events for WETH contract\n weth_contract_address = Web3.to_checksum_address('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2')\n deposit_topic = \"0x\" + keccak.new(data=b'Deposit(address,uint256)', digest_bits=256).hexdigest()\n withdrawal_topic = \"0x\" + keccak.new(data=b'Withdrawal(address,uint256)', digest_bits=256).hexdigest()\n subscription_id = await provider.subscribe(\n [\n 'logs',\n {\n \"address\": weth_contract_address,\n \"topics\": [deposit_topic, withdrawal_topic]\n }\n ],\n callback\n )\n print(f'Subscribed with id {subscription_id}')\n \n # unsubscribe after 30 seconds\n await asyncio.sleep(30)\n await provider.unsubscribe(subscription_id)\n\nif __name__ == '__main__':\n async_loop = asyncio.get_event_loop()\n async_loop.run_until_complete(main(loop=async_loop))\n```\n\n\n[pypi_version]: https://img.shields.io/pypi/v/web3-proxy-providers.svg \"PYPI version\"\n[licence_version]: https://img.shields.io/badge/license-MIT%20v2-brightgreen.svg \"MIT Licence\"\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Proxy providers for web3py (Web3 python)",
"version": "0.3",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9c6a803f4a59180c5b75889a235c07e3023712286027d38b77a451472da2c6ea",
"md5": "61a956c1379fe06f7be927179b59fe88",
"sha256": "cbe3df699dd9e672937b79fdc64b489e6edacd39046dbb955c8267fce8471ce2"
},
"downloads": -1,
"filename": "web3_proxy_providers-0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "61a956c1379fe06f7be927179b59fe88",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 14475,
"upload_time": "2023-02-11T08:53:40",
"upload_time_iso_8601": "2023-02-11T08:53:40.356920Z",
"url": "https://files.pythonhosted.org/packages/9c/6a/803f4a59180c5b75889a235c07e3023712286027d38b77a451472da2c6ea/web3_proxy_providers-0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2733d584c4dcaa70fd0100d476a45b5cccd4d8121e680b76065701bd94889bdb",
"md5": "56add8a3049925a375e9698b267d2e82",
"sha256": "87002313e6017028bd012d8b3f8b5fbe124eb2a1d37697586723ce0d35ed55bc"
},
"downloads": -1,
"filename": "web3-proxy-providers-0.3.tar.gz",
"has_sig": false,
"md5_digest": "56add8a3049925a375e9698b267d2e82",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 10931,
"upload_time": "2023-02-11T08:53:42",
"upload_time_iso_8601": "2023-02-11T08:53:42.030464Z",
"url": "https://files.pythonhosted.org/packages/27/33/d584c4dcaa70fd0100d476a45b5cccd4d8121e680b76065701bd94889bdb/web3-proxy-providers-0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-11 08:53:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "sinarezaei",
"github_project": "web3_proxy_providers",
"lcname": "web3-proxy-providers"
}