Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Python server script
import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
# Bind socket to local host and port
try:
s.bind(('0.0.0.0', 4010))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
# Start listening on socket
s.listen(1)
# Wait to accept a connection - blocking call
conn, addr = s.accept()
result = ''
try:
result = conn.recv(1024)
except socket.timeout:
print 'timeout 1'
print 'first: ' + str(result)
msg = "bla"
conn.send(msg)
try:
result = conn.recv(1024)
except socket.timeout:
print 'timeout 2'
print 'second: ' + str(result)
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment