python - 在 Debian 系统上登录时运行一个简单的 Python TCP 服务器

标签 python tcp debian

我试图在启动时运行一个简单的服务器。我使用的操作系统是 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/

相关文章:

python - 将动态生成的模型添加到 Django 项目中的 models.py

tcp - 通过 TCP WiFi 在 25 台设备上连接并运行 adb 命令,而无需连接任何 USB

python - Opencv:如何计算 bolt 上的线程峰?

actionscript-3 - Adobe AIR : ServerSocketConnectEvent not firing for localhost connections

java - HttpUrlConnection 和 setReadTimeout() 方法

networking - 问题设置静态 ETH0 IP BeagleBone Black Version C Debian Preloaded

linux - 如何在不重启或注销的情况下更新 sudo bash 中的/etc/profile

linux - 使用终端监控 linux 服务器和/或日志文件

python - 理解字符串格式中的 Python %g,实现 Java String.format 行为

python - 如何使用 Webdriver 和 Python 与此模式对话框进行交互?