How do i use proxy in web3py?

Question:

I’m currently using this method to connect to web3py

infura_url = "https://node.node/asdasdasdasdasd/bsc/mainnet/archive/" # fast
web3 = Web3(Web3.HTTPProvider(infura_url))

How do I enable proxy? say i have proxy at ‘321.123.121.123:8081’

Asked By: user40780

||

Answers:

I = "http://aaa:[email protected]:20000a"
infura_url = "https://zzzz.io/kkkk/bsc/mainnet/archive/" # fast
web3 = Web3(Web3.HTTPProvider(infura_url,request_kwargs={"proxies":{'https' : I, 'http' : I }}))
Answered By: user40780

You can also use web3-proxy-providers, which is a python package for connecting to HTTP and WebSocket Json-RPC using Socks and HTTP proxies

run

pip install web3-proxy-providers

code

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)

Disclaimer: I am the owner of this package
https://github.com/sinarezaei/web3-proxy-providers

Answered By: Sina Rezaei
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.