我试图在启动时运行一个简单的服务器。我使用的操作系统是 Debian 6.0。我在我的 .profile 中添加了一行来运行 python 脚本:python/root/desktopnavserver2.py
计算机启动并登录,但是我收到以下错误。当我在 debians.profile 中没有添加行并且我只是在控制台中自己运行脚本时,脚本运行良好。有什么帮助吗?
错误跟踪:
Traceback (most recent call last):
File "/root/Desktop/navserver2.py", line 39, in <module>
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
File "/usr/lib/python2.6/SocketServer.py", line 402, in __init__
self.server_bind()
File "/usr/lib/python2.6/SocketServer.py", line 413, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
error: [Errno 98] Address already in use
来源:
#!/usr/bin/python
import SocketServer
import serial
com2 = serial.Serial(
port = 1,
parity = serial.PARITY_NONE,
bytesize = serial.EIGHTBITS,
stopbits = serial.STOPBITS_ONE,
timeout=3,
xonxoff = 0,
rtscts = 0,
baudrate = 9600
)
class MyTCPHandler(SocketServer.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
#print "%s wrote:"%self.client_address[0]
#print self.data
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
com2.write(self.data)
if __name__ == "__main__":
HOST, PORT = "192.168.0.200", 14052 #change to 192.168.0.200
# Create the server, binding to localhost on port 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
最佳答案
行 error: [Errno 98] Address already in use
对此进行了解释 - 在进入您的程序时找出该端口正在运行的内容(提示:.py 是否被调用了两次?)
关于python - 在 Debian 系统上登录时运行一个简单的 Python TCP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9702933/