简体   繁体   中英

Running a Simple Python TCP Server at Login on a Debian System

I am trying to run a simple server at start up. The OS I am using is Debian 6.0. I added a line in my .profile to run the python script: python /root/desktopnavserver2.py The computer boots and logs in, however I get the error below. The script runs fine when I don't have the add line in debians .profile and I just run the script myself in console. Any Help?

Error Trace:

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

Source:

#!/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被调用两次?)

I believe that .profile is executed by every instance of shell that identifies itself as a login shell. Probably you have more than one such shell.

Putting a single-instance script in your .profile is a bad idea anyway - it means that you can only have one login session, next sessions will cause this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM