Skip to content

Instantly share code, notes, and snippets.

@tzhenghao
Created January 17, 2018 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzhenghao/e70700174e76a0e184642c5a82165937 to your computer and use it in GitHub Desktop.
Save tzhenghao/e70700174e76a0e184642c5a82165937 to your computer and use it in GitHub Desktop.
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