It was indeed a port issue. Unix Domain Sockets nailed it.
Python ThreadedTCPServer: "Name or service not known"
Pergunta
I was developing a ThreadedTCPServer to communicate with a PHP application also residing in this same machine. This is suppose to receive requests from this PHP app and to convert some videos locally using ffmpeg.
Here's the code:
# -*- 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()
This works well in my development laptop, but on the server i'm getting the following error:
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
I'm guessing it has to do with the port I'm using (6666), but I've tried others and it hasn't been working. Would Unix Domain Sockets be of use here? Can you give me an example?
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow