-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto web3 #344
Auto web3 #344
Changes from 7 commits
facd755
52e9151
51d5abb
7726190
49e0d10
12c23d8
26de091
daed896
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import importlib | ||
|
||
for connector in ('ipc', 'http'): | ||
connection = importlib.import_module('web3.auto.' + connector) | ||
if connection.w3: | ||
w3 = connection.w3 | ||
|
||
if w3.isConnected(): | ||
break |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from web3 import ( | ||
HTTPProvider, | ||
Web3 | ||
) | ||
import os | ||
|
||
|
||
def connect(): | ||
|
||
w3 = Web3(HTTPProvider( | ||
os.environ.get('WEB3_HTTP_PROVIDER_URI', 'http://localhost:8545') | ||
)) | ||
if w3.isConnected(): | ||
return w3 | ||
else: | ||
return False | ||
|
||
|
||
w3 = connect() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from web3.providers.ipc import get_default_ipc_path | ||
from web3 import ( | ||
IPCProvider, | ||
Web3 | ||
) | ||
|
||
|
||
def connect(): | ||
w3 = Web3(IPCProvider(get_default_ipc_path())) | ||
if w3.isConnected(): | ||
return w3 | ||
|
||
return False | ||
|
||
|
||
w3 = connect() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,22 +42,52 @@ def get_default_ipc_path(testnet=False): | |
testnet = "" | ||
|
||
if sys.platform == 'darwin': | ||
return os.path.expanduser(os.path.join( | ||
ipc_path = os.path.expanduser(os.path.join( | ||
"~", | ||
"Library", | ||
"Ethereum", | ||
testnet, | ||
"geth.ipc", | ||
"geth.ipc" | ||
)) | ||
if os.path.exists(ipc_path): | ||
return ipc_path | ||
|
||
ipc_path = os.path.expanduser(os.path.join( | ||
"~", | ||
"Library", | ||
"Application Support", | ||
"io.parity.ethereum", | ||
"jsonrpc.ipc" | ||
)) | ||
if os.path.exists(ipc_path): | ||
return ipc_path | ||
|
||
elif sys.platform.startswith('linux'): | ||
return os.path.expanduser(os.path.join( | ||
ipc_path = os.path.expanduser(os.path.join( | ||
"~", | ||
".ethereum", | ||
testnet, | ||
"geth.ipc", | ||
"geth.ipc" | ||
)) | ||
if os.path.exists(ipc_path): | ||
return ipc_path | ||
|
||
ipc_path = os.path.expanduser(os.path.join( | ||
"~", | ||
".local", | ||
"share", | ||
"io.parity.ethereum", | ||
"jsonrpc.ipc" | ||
)) | ||
if os.path.exists(ipc_path): | ||
return ipc_path | ||
|
||
elif sys.platform == 'win32': | ||
return "\\\\.\\pipe\\geth.ipc" | ||
if os.path.exists("\\\\.\\pipe\\geth.ipc"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these should still use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heh, I've been out of windows for so long that I don't remember what @Sebohe because of the '\' start, it wasn't intuitive to me how to use join here. This was the first way I could find to do the join: It looks like this is the last change needed to get merged. |
||
return "\\\\.\\pipe\\geth.ipc" | ||
elif os.path.exists("\\\\.\\pipe\\jsonrpc.ipc"): | ||
return "\\\\.\\pipe\\jsonrpc.ipc" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A utility function like |
||
|
||
else: | ||
raise ValueError( | ||
"Unsupported platform '{0}'. Only darwin/linux2/win32 are " | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check looks redundant: if w3 is set, then
isConnected()
is True