MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / BitcoinHTTPConnection

Class BitcoinHTTPConnection

test/functional/interface_http.py:29–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27)
28
29class BitcoinHTTPConnection:
30 def __init__(self, node):
31 self.url = urllib.parse.urlparse(node.url)
32 self.authpair = f'{self.url.username}:{self.url.password}'
33 self.headers = {"Authorization": f"Basic {str_to_b64str(self.authpair)}"}
34 self.reset_conn()
35
36 def reset_conn(self):
37 self.conn = http.client.HTTPConnection(self.url.hostname, self.url.port)
38 self.conn.connect()
39
40 def sock_closed(self):
41 if self.conn.sock is None:
42 return True
43 try:
44 self.conn.request('GET', '/')
45 self.conn.getresponse().read()
46 return False
47 except NETWORK_ERRORS:
48 return True
49
50 def close_sock(self):
51 self.conn.close()
52
53 def set_timeout(self, seconds):
54 self.conn.sock.settimeout(seconds)
55
56 def add_header(self, key, value):
57 self.headers.update({key: value})
58
59 def _request(self, method, path, data, connection_header, **kwargs):
60 headers = self.headers.copy()
61 if connection_header is not None:
62 headers["Connection"] = connection_header
63 self.conn.request(method, path, data, headers, **kwargs)
64 return self.conn.getresponse()
65
66 def post(self, path, data, connection_header=None, **kwargs):
67 return self._request('POST', path, data, connection_header, **kwargs)
68
69 def get(self, path, connection_header=None):
70 return self._request('GET', path, '', connection_header)
71
72 def send_raw(self, data):
73 self.conn.sock.sendall(data)
74
75 def post_raw(self, path, data):
76 data_bytes = data.encode("utf-8")
77 req = f"POST {path} HTTP/1.1\r\n"
78 req += f'Authorization: Basic {str_to_b64str(self.authpair)}\r\n'
79 req += f'Content-Length: {len(data_bytes)}\r\n\r\n'
80 self.send_raw(req.encode("ascii") + data_bytes)
81
82 def recv_raw(self):
83 '''
84 Blocking socket will wait until data is received and return up to 1024 bytes
85 '''
86 return self.conn.sock.recv(1024)

Calls

no outgoing calls

Tested by

no test coverage detected