<p>I think you have your magic byte order backwards. It should be 0xf9beb4d9.</p>

CC BY-SA 4.0
2020-08-30T19:11:21.983Z
2020-08-30T19:11:21.983Z
Oscar Serna
<p>I am trying to implement an SPV client in python (for my own learning). I want to start by making a simple TCP connection to a full node, but I either cannot get a response to my version message, or to my verack message if the version message goes through. I have referenced <a href="https://dev.to/alecbuda/introduction-to-the-bitcoin-network-protocol-using-python-and-tcp-sockets-1le6" rel="nofollow noreferrer">this</a> and this is the code that I am trying to run:</p>
<pre><code>#Import dependencies
import socket
import time
import random
import struct
import hashlib
import binascii
import ssl
import socks

# Binary encode the sub-version
def create_sub_version():
    sub_version = &quot;/Satoshi:0.7.2/&quot;
    return b'\x0F' + sub_version.encode()

# Binary encode the network addresses
def create_network_address(ip_address, port):
    network_address = struct.pack('&gt;8s16sH', b'\x01', 
        bytearray.fromhex(&quot;00000000000000000000ffff&quot;) + socket.inet_aton(ip_address), port)
    return(network_address)

# Create the TCP request object
def create_message(magic, command, payload):
    checksum = hashlib.sha256(hashlib.sha256(payload).digest()).digest()[0:4]
    return(struct.pack('L12sL4s', magic, command.encode(), len(payload), checksum) + payload)

# Create the &quot;version&quot; request payload
def create_payload_version(peer_ip_address):
    version = 60002
    services = 1
    timestamp = int(time.time())
    addr_local = create_network_address(&quot;127.0.0.1&quot;, 8333)
    addr_peer = create_network_address(peer_ip_address, 8333)
    nonce = random.getrandbits(64)
    start_height = 645953
    payload = struct.pack('&lt;LQQ26s26sQ16sL', version, services, timestamp, addr_peer,
                          addr_local, nonce, create_sub_version(), start_height)
    return(payload)

# Create the &quot;verack&quot; request message
def create_message_verack():
    return bytearray.fromhex(&quot;f9beb4d976657261636b000000000000000000005df6e0e2&quot;)


# Create the &quot;getdata&quot; request payload
def create_payload_getdata(tx_id):
    count = 1
    type = 1
    hash = bytearray.fromhex(tx_id)
    payload = struct.pack('&lt;bb32s', count, type, hash)
    return(payload)

# Print request/response data
def print_response(command, request_data, response_data):
    print(&quot;&quot;)
    print(&quot;Command: &quot; + command)
    print(&quot;Request:&quot;)
    print(binascii.hexlify(request_data))
    print(&quot;Response:&quot;)
    print(binascii.hexlify(response_data))

if __name__ == '__main__':
    # Set constants
    magic_value = 0xd9b4bef9
    tx_id = &quot;a13e7979d3607e187d2ce81ca9fdadfe967b2f3fc1109ce47d46cc09ebd41c50&quot;
    peer_ip_address = socket.gethostbyname('bitcoin.aranguren.org') 
    peer_tcp_port =  50002
    buffer_size = 1024

    # Create Request Objects
    version_payload = create_payload_version(peer_ip_address)
    version_message = create_message(magic_value, 'version', version_payload)
    verack_message = create_message_verack()
    getdata_payload = create_payload_getdata(tx_id)
    getdata_message = create_message(magic_value, 'getdata', getdata_payload)

    # Establish TCP Connection
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(10)
    ws = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_TLSv1)
    ws.connect((peer_ip_address, peer_tcp_port))

    # Send message &quot;version&quot;
    ws.send(version_message)
    response_data = ws.recv(buffer_size)
    print_response(&quot;version&quot;, version_message, response_data)

    # Send message &quot;verack&quot;
    ws.send(verack_message)
    response_data = ws.recv(buffer_size)
    print_response(&quot;verack&quot;, verack_message, response_data)

    # Send message &quot;getdata&quot;
    ws.send(getdata_message)
    response_data = ws.recv(buffer_size)
    print_response(&quot;getdata&quot;, getdata_message, response_data)

    # Close the TCP connection
    ws.close()
</code></pre>
<p>The server I connect to in the code is among the only ones that would respond to the version message, returning b'7b226a736f6e727063223a2022322e30222c20226572726f72223a207b22636f6465223a202d33323730302c20226d657373616765223a20226d65737361676573206d75737420626520656e636f64656420696e205554462d38227d2c20226964223a206e756c6c7d0a'.</p>
<p>I also tried referencing: <a href="https://bitcoin.stackexchange.com/questions/50444/unable-to-send-version-message-correctly">this stack exchange question</a> and <a href="https://github.com/shirriff/bitcoin-code" rel="nofollow noreferrer">Ken Shirriff's github code</a>, but none seem to work, either because I am using python3 or from other causes. I am a beginner in this, so if somebody could help me understand why I cannot get my messages across in the above code or has another implementation in python3, I would be grateful.</p>

CC BY-SA 4.0
2020-08-30T16:09:08.603Z
It is probably helpful to run a full node with debugging enabled (e.g. Bitcoin Core with `-debug=net`), and connect to that. The output in debug.log may tell you what you're doing wrong.
Pieter Wuille
CC BY-SA 4.0
2020-08-30T19:03:18.100Z
Have you tried with magic = f9beb4d9?
Oscar Serna
CC BY-SA 4.0
2020-08-30T19:37:56.093Z
This is the message you are getting back: {"code": -32700, "message": "messages must be encoded in UTF-8"}, "id": null
Oscar Serna
<python><spv><connection><tcp>
SPV client TCP connection in python
anne1234
# SPV client TCP connection in python

(Asked by: username_0 on 2020-08-30)

I am trying to implement an SPV client in python (for my own learning). I want to start by making a simple TCP connection to a full node, but I either cannot get a response to my version message, or to my verack message if the version message goes through. I have referenced this and this is the code that I am trying to run:
#Import dependencies
import socket
import time
import random
import struct
import hashlib
import binascii
import ssl
import socks

# Binary encode the sub-version
def create_sub_version():
    sub_version = "/Satoshi:0.7.2/"
    return b'\x0F' + sub_version.encode()

# Binary encode the network addresses
def create_network_address(ip_address, port):
    network_address = struct.pack('>8s16sH', b'\x01', 
        bytearray.fromhex("00000000000000000000ffff") + socket.inet_aton(ip_address), port)
    return(network_address)

# Create the TCP request object
def create_message(magic, command, payload):
    checksum = hashlib.sha256(hashlib.sha256(payload).digest()).digest()[0:4]
    return(struct.pack('L12sL4s', magic, command.encode(), len(payload), checksum) + payload)

# Create the "version" request payload
def create_payload_version(peer_ip_address):
    version = 60002
    services = 1
    timestamp = int(time.time())
    addr_local = create_network_address("127.0.0.1", 8333)
    addr_peer = create_network_address(peer_ip_address, 8333)
    nonce = random.getrandbits(64)
    start_height = 645953
    payload = struct.pack('
The server I connect to in the code is among the only ones that would respond to the version message, returning b'7b226a736f6e727063223a2022322e30222c20226572726f72223a207b22636f6465223a202d33323730302c20226d657373616765223a20226d65737361676573206d75737420626520656e636f64656420696e205554462d38227d2c20226964223a206e756c6c7d0a'.
I also tried referencing: this stack exchange question and Ken Shirriff's github code, but none seem to work, either because I am using python3 or from other causes. I am a beginner in this, so if somebody could help me understand why I cannot get my messages across in the above code or has another implementation in python3, I would be grateful.

**Tags:** python, spv, connection, tcp

> **Comments:**
> **username_1**: It is probably helpful to run a full node with debugging enabled (e.g. Bitcoin Core with `-debug=net`), and connect to that. The output in debug.log may tell you what you're doing wrong.  
> **username_2**: Have you tried with magic = f9beb4d9?  
> **username_2**: This is the message you are getting back: {"code": -32700, "message": "messages must be encoded in UTF-8"}, "id": null  

---

## 1 answers

### Answer by username_2 on 2020-08-30. Score: 0

I think you have your magic byte order backwards. It should be 0xf9beb4d9.


---

