Skip to content

Instantly share code, notes, and snippets.

@tushartyagi
Created March 10, 2017 13:07
Show Gist options
  • Save tushartyagi/ef91cd29d37ac39b29d5a390b2ed1046 to your computer and use it in GitHub Desktop.
Save tushartyagi/ef91cd29d37ac39b29d5a390b2ed1046 to your computer and use it in GitHub Desktop.
Echo socket client in python
# Taken from Python MOTW
import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = ('localhost', 5678)
s.connect(server)
print('Connecting to {} at port {}', *server)
try:
message = b'This is a message. This will be repeated'
print('Sending message: {}', message)
s.sendall(message)
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = s.recv(16)
amount_received += len(data)
print('received {!r}'.format(data))
finally:
print('closing socket')
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment