2012-03-14 2 views
0

시작시 간단한 서버를 실행하려고합니다. 현재 사용중인 OS는 Debian 6.0입니다. 내 .profile에 python 스크립트를 실행하는 줄을 추가했습니다. python /root/desktopnavserver2.py 컴퓨터가 부팅되고 로그인되지만 아래 오류가 나타납니다. debian.profile에 줄을 추가하지 않아도 스크립트가 잘 실행되고 콘솔에서 직접 스크립트를 실행합니다. 어떤 도움?데비안 시스템에서 로그인 할 때 간단한 파이썬 TCP 서버 실행

오류 추적 :

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() 

답변

1

나는 .profile 로그인 쉘로 자신을 식별하는 쉘의 모든 인스턴스에 의해 실행됩니다. 아마 당신은 하나 이상의 쉘을 가지고있을 것입니다.

단일 인스턴스 스크립트를 .profile에 넣는 것은 어쨌든 나쁜 생각입니다. 즉, 하나의 로그인 세션 만 가질 수 있음을 의미합니다. 다음 세션에서는이 오류가 발생합니다.

3

그것이 라인 error: [Errno 98] Address already in use은 설명합니다 - 프로그램 (힌트에 항목에 해당 포트에서 실행중인 것을 찾을 다음이다 .py가 두 번 호출되는 이유는 무엇입니까?)

+0

가끔은 스크립트가 제대로 실행되고 그렇지 않은 경우가 있습니다. 실제로 스크립트가 두 번 실행되는 것처럼 들리지만 그 이유는 확실하지 않습니다. 이 오류가 발생하여 lsof -i를 입력하면 동일한 포트를 사용하는 python 스크립트가 있음을 알 수 있습니다. 문제는 스크립트를 시작하는 .profile에서 행을 제거하면 실행중인 스크립트의 인스턴스가 없다는 것입니다. – Richard

+0

여러 터미널/창을 실행 중입니까? 그렇다면 .profile의'ps -ef' 라인을 통해 스크립트를 실행할 때 스크립트가 아직 실행 중이 아닌지 확인해야합니다 – KevinDTimm

관련 문제