Here follow a simple python server using socket module:
import socket
import sys
HOST = ''
PORT = 8008
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.bind((HOST,PORT))
except socket.error as msg:
print 'Bind failed. Error code : %s , Message : %s'%(msg[0],msg[1])
sys.exit()
print 'Socket bind complete!'
s.listen(10)
socket.setdefaulttimeout(3)
l = set()
while True:
try:
a = s.accept()
print 'Connected with %s:%d'%a[1]
l.add(a)
except:
print 'Accept error!'
for a in l:
b = a[0].recv(4096)
if b:
print 'From %s:%d'%a[1]+' recv: %s'%b
It was correct at the first time(connection by client), BUT the program stuck at the second time.
Socket bind complete!
Connected with 127.0.0.1:52093
From 127.0.0.1:52093 recv: aasdf
_
(STUCK)
What was wrong? Please point out the problem for me