Created
January 17, 2018 05:55
-
-
Save tzhenghao/e70700174e76a0e184642c5a82165937 to your computer and use it in GitHub Desktop.
Simple Python server script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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