Python放入Multiprocessing.Queue后无法重建SSL套接字

时间:2013-05-24 16:10:24

标签: python ssl python-2.7 openssl

我有一个服务器,其中有一个进程在等待连接,然后将套接字放入队列中,因此工作线程可以获取连接并与客户端通信并执行任何操作。

接收流程

    while True:
        # Wait for a connection
        print >>sys.stdout, 'waiting for a connection'

        readySocks = select.select(socksToCheck, [], [], d_consts.SELECT_SEC)
        for s in readySocks[0]:
            if s is serverClient:
                sock, client_address = s.accept()
                print >>sys.stdout, 'Client connection from ', client_address

                sockfd = multiprocessing.reduction.reduce_handle(sock.fileno())
                self.cmpQueue.put(inter_proc_msgs.ConMsg(sockfd, client_address))
                print >>sys.stdout, 'Client connection sent for processing: ', client_address

工作人员流程

        try:
            print '[%s] Checking Queue...' % self.name
            clientConMsg = self.cmpQueue.get(block=False) #throws exception if empty
            print 'something in queue'
            fileDescriptor = multiprocessing.reduction.rebuild_handle(clientConMsg.sockfd )
            newCon = socket.fromfd(fileDescriptor, socket.AF_INET, socket.SOCK_STREAM)
            print self.name, newCon, time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())

            #wrap in ssl
            sslsock = ssl.wrap_socket(newCon,
                                 server_side=True,
                                 certfile="newcert.pem",
                                 keyfile="privkey.pem",
                                 ssl_version=ssl.PROTOCOL_SSLv23)
            ##non blocking
            sslsock.setblocking(0)
            #add to the list of client sockets to check with TTL
            self.clientSocketsWTTL.appendleft(self.createTupleSocknTTL(newCon, clientConMsg.clientAdd))

        except Queue.Empty:
            print '[%s] X Nothing in Queue...' % self.name 
            #pass

现在这在我放SSL之前就已经工作了,但是现在使用SSL我得到了:

回溯

Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/home/max/workspace/Dealer/src/client_msg_processor.py", line 35, in run
    self.processClientSideTasks()
  File "/home/max/workspace/Dealer/src/client_msg_processor.py", line 66, in processClientSideTasks
    self.testSSL()
  File "/home/max/workspace/Dealer/src/client_msg_processor.py", line 113, in testSSL
    ssl_version=ssl.PROTOCOL_SSLv23)
  File "/usr/lib/python2.7/ssl.py", line 381, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 111, in __init__
    socket.__init__(self, _sock=sock._sock)
AttributeError: '_socket.socket' object has no attribute '_sock'

知道这是否可以修复?我拼命想避免重组整个服务器。

1 个答案:

答案 0 :(得分:1)

执行newCon = socket.socket(_sock=newCon)修复了问题。

请参阅http://www.velocityreviews.com/forums/t557014-socket-vs-_socketobject.html