我目前正在尝试了解Python中的套接字,并且我正在使用以下代码接收通过TCP从热点网络上的智能手机传输的数据。奇怪的Errno 48地址已在使用行为python
import socket
import logging
# Logging routine
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 5000)
logger.info('starting up on %s port %s' % server_address)
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
# Wait for a connection
logger.info('waiting for a connection')
connection, client_address = sock.accept()
try:
logger.info('connection from', client_address)
while True:
data = connection.recv(16)
if data:
print 'Do stuff here'
else:
print 'no more data from', client_address
break
finally:
# Clean up the connection
connection.close()
我第一次运行代码一切正常。运行脚本并希望再次运行后,出现以下错误:[Errno 48] Address already in use?
。但是,如果我在程序崩溃后再次运行脚本,它将正常运行。我已经检查并确认它在connection.close()
的最终声明上结束。看来我总是要跑两次才能连续跑两次。
在每次运行后,在shell提示执行此命令:'的netstat -an | egrep:5000'。让我们知道它说了什么。 –
@Rob提供的命令不会产生输出。 – dudas
我的请求是在每次运行后运行该命令。你描述的测试运行你的命令三次。在你描述的三个运行中的哪一个之后,该命令不产生输出? –