python的sokcet长连接
如何实现Python的socket的长连接呢?另外:if__name__=='__main__':importsocketimportsyssock=socket.soc...
如何实现Python的socket的长连接呢?
另外:
if __name__ == '__main__':
import socket
import sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 7660))
sock.listen(5)
connection,address = sock.accept()
while True:
try: ^M
connection.settimeout(5)
buf = connection.recv(1024)
print 'Server receive data is', buf
connection.send('success::')
except socket.timeout:
print 'time out'
#connection.close()
上面的代码,为什么在执行完print 'Server receive data is', buf之后,再执行connection.send('success::')会发生阻塞呢?数据为什么没有发送出去呢?(当然加上最后的connection.close()是可以的,我的意思是能是长连接) 展开
另外:
if __name__ == '__main__':
import socket
import sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 7660))
sock.listen(5)
connection,address = sock.accept()
while True:
try: ^M
connection.settimeout(5)
buf = connection.recv(1024)
print 'Server receive data is', buf
connection.send('success::')
except socket.timeout:
print 'time out'
#connection.close()
上面的代码,为什么在执行完print 'Server receive data is', buf之后,再执行connection.send('success::')会发生阻塞呢?数据为什么没有发送出去呢?(当然加上最后的connection.close()是可以的,我的意思是能是长连接) 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询