Echo Client

Sample TCP Client

>>> import socket
>>> sock = socket(socket.AF_INET, socket.SOCK_STREAM)

>>> # connect to server on localhost
>>> # listening on port 8881
>>> sv_addr = ('localhost', 8881)
>>> sock.connect(sv_addr)
>>> print('connected to server')

>>> data = """
... A few lines of data
... to test the operation
... of both server and client.
... """

>>> for line in data.splitlines():
...     sock.sendall(line)  # send data to server
...     print('sent: ', line)
...     response = sock.recv(8192)     # receive data...
...     print('received: ', response)  # ...returned...
>>> sock.close()                       # ...from server

References

Previous

Echo Server