Python ThreadedTCPServer:"名称或服务未知"

时间:2014-04-10 17:11:29

标签: python sockets tcp ffmpeg daemon

我正在开发一个ThreadedTCPServer来与也位于同一台机器上的PHP应用程序进行通信。这是为了接收来自这个PHP应用程序的请求,并使用ffmpeg在本地转换一些视频。

以下是代码:

# -*- coding: utf-8 -*- 
import os
import socket
import threading
import logging.config
import SocketServer, time
from queuev2 import QueueServer

logging.basicConfig(format='[%(asctime)s.%(msecs).03d] %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filename=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'converter.log'), level=logging.INFO)

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        data = self.request.recv(1024)
        cur_thread = threading.current_thread()
        response = "{}: {}".format(cur_thread.name, data)
        videoPool.add(data)
        print "Output! %s" % data
        self.request.sendall(response)

class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    pass

if __name__ == "__main__":

    logging.info("Initializing...")
    videoPool = QueueServer()
    HOST, PORT = "localhost", 6666

    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
    ip, port = server.server_address

    # Start a thread with the server -- that thread will then start one
    # more thread for each request
    server_thread = threading.Thread(target=server.serve_forever)

    # Exit the server thread when the main thread terminates
    server_thread.daemon = True
    server_thread.start()

    print("Server loop running in thread: %s" % server_thread.name)

    # "Groundhog day" time
    while True:
        time.sleep(999)
        pass

    #server.shutdown()

这在我的开发笔记本电脑上运行良好,但在服务器上我收到以下错误:

Traceback (most recent call last):
  File "server.py", line 31, in <module>
    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
  File "/usr/lib/python2.7/SocketServer.py", line 408, in __init__
    self.server_bind()
  File "/usr/lib/python2.7/SocketServer.py", line 419, in server_bind
    self.socket.bind(self.server_address)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.gaierror: [Errno -2] Name or service not known

我猜这与我正在使用的端口有关(6666),但我已经尝试了其他的并且它没有工作。 Unix域套接字会在这里使用吗?你能举个例子吗?

1 个答案:

答案 0 :(得分:0)

这确实是一个港口问题。 Unix域套接字钉它。