MCPcopy
hub / github.com/pyodide/pyodide / test_socket_connect

Function test_socket_connect

src/tests/test_python_socket.py:58–88  ·  view source on GitHub ↗

Test that Python socket can connect to a server and exchange data.

(selenium_nodesock)

Source from the content-addressed store, hash-verified

56
57
58def test_socket_connect(selenium_nodesock):
59 """Test that Python socket can connect to a server and exchange data."""
60 TEST_MESSAGE = b"Hello from client"
61 RESPONSE_MESSAGE = b"Hello from server"
62
63 server_received = []
64
65 def handler(conn, _addr):
66 data = conn.recv(1024)
67 server_received.append(data)
68 conn.sendall(RESPONSE_MESSAGE)
69
70 @run_in_pyodide
71 async def run(selenium, host, port, message):
72 import socket
73
74 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
75 s.connect((host, port))
76
77 s.sendall(message)
78
79 response = s.recv(1024)
80
81 s.close()
82 return response.decode()
83
84 with tcp_server(handler) as (host, port):
85 result = run(selenium_nodesock, host, port, TEST_MESSAGE)
86 assert len(server_received) == 1
87 assert server_received[0] == TEST_MESSAGE
88 assert result == RESPONSE_MESSAGE.decode()
89
90
91def test_socket_multiple_send_recv(selenium_nodesock):

Callers

nothing calls this directly

Calls 2

tcp_serverFunction · 0.85
runFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…