Score:1

Network connection problems with Python3 and bitcoinrpc in WSL2 but not WSL1

in flag

I am running Ubuntu 20.04 on Windows 10 using WSL2.

I am trying to connect to bitcoin testnet server on my machine 127.0.0.1 through Python

The python scripts work fine on Pycharm, Command Prompt and Powershell, and up until very recently worked fine in Ubuntu when i was runnign it on WSL1

Having switched to WSL2 for Ubuntu I receive the following error despite at the same working in CMD, PS, and Pycharm. Seems there's something blocking on the Ubuntu side. How do I fix this? Disabling Windows Fast Start didn't solve this issue.

Traceback (most recent call last):
  File "/home/lalexk/mpcs56600-work/Lab4/Lab4.py", line 308, in <module>
    run_lab_4()
  File "/home/lalexk/mpcs56600-work/Lab4/Lab4.py", line 236, in run_lab_4
    block_info = connect_rpc(which_port, rpc_username, rpc_password, block_to_get)
  File "/home/lalexk/mpcs56600-work/Lab4/Lab4.py", line 41, in connect_rpc
    return rpc_connection.getblock(block_hash)
  File "/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.9.6/lib/python3.9/site-packages/bitcoinrpc/authproxy.py", line 132, in __call__
    self.__conn.request('POST', self.__url.path, postdata,
  File "/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.9.6/lib/python3.9/http/client.py", line 1257, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.9.6/lib/python3.9/http/client.py", line 1303, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.9.6/lib/python3.9/http/client.py", line 1252, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.9.6/lib/python3.9/http/client.py", line 1012, in _send_output
    self.send(msg)
  File "/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.9.6/lib/python3.9/http/client.py", line 952, in send
    self.connect()
  File "/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.9.6/lib/python3.9/http/client.py", line 923, in connect
    self.sock = self._create_connection(
  File "/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.9.6/lib/python3.9/socket.py", line 843, in create_connection
    raise err
  File "/home/linuxbrew/.linuxbrew/Cellar/[email protected]/3.9.6/lib/python3.9/socket.py", line 831, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
Score:0
vn flag

I'm not all that familiar with these scripts, but it sounds like you are saying that the Bitcoin server is running on your Windows host, and you are trying to connect to it at 127.0.0.1 from inside WSL2. That would, at least, explain what you are experiencing.

The difference between WSL1 and WSL2 in this case is:

  • WSL1 shared the Windows network in a very simple fashion. A WSL1 instance has the same IP address as that of its Windows host, and 127.0.0.1/localhost from inside WSL1 is the same as the Windows localhost. It acts like a bridged network.

  • WSL2, on the other hand, runs in a more virtualized environment, primarily using elements of the Windows Hyper-V hypervisor. Its network is on a virtual NIC that runs NAT'd behind the Windows host. 127.0.0.1/localhost refers to the WSL2 virtual network adapter, not that of its Windows host.

The solution should be fairly straightforward. Use either:

  • The address of the Windows host (e.g. 192.168.1.10)
  • The address of the virtual router that Hyper-V sets up, which can be determined with ip r l default in WSL.
  • Or the mDNS name for that virtual router. This is the Windows "Computer Name" followed by .local. If you need to determine that name from inside WSL2, you can use:
    echo "$(powershell.exe '[Console]::Write($env:COMPUTERNAME)').local"
    

If I'm misunderstanding your setup and that doesn't work, let me know and we can clarify the configuration.

Tennis Tubbies avatar
in flag
Thanks a lot, you've helped enormously. I can see why it doesn't work now.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.