如何序列化 python 中的 httplib SSL 连接对象?

How to serialize a httplib SSL connection object in python?

我试图通过 zmq 套接字发送一个 httplib 连接对象,但我收到以下错误 TypeError: can't pickle _ssl._SSLSocket objects

现在我认为情况会是这样,因为我目前只是在试验,但是有没有人知道包装或序列化 SSL 对象的方法,以便我可以通过套接字将它发送到其他线程?

示例用例:

#Script 1
import zmq
ctx = zmq.Context()
GLOBAL_SOCKET=ctx.socket(zmq.PUB)
GLOBAL_SOCKET.bind('tcp://127.0.0.1:2000')

conn = httplib.HTTPSConnection('site...')
GLOBAL_SOCKET.send_pyobj(conn)


#script 2
import zmq
ctx = zmq.Context()
GLOBAL_SOCKET=ctx.socket(zmq.SUB)
GLOBAL_SOCKET.connect('tcp://127.0.0.1:2000')
GLOBAL_SOCKET.setsockopt(zmq.SUBSCRIBE, '')

#recv SSL object in response hopefully
conn_ = GLOBAL_SOCKET.recv_pyobj()

#work with the connection object in another thread
conn_.request('GET', ....)
conn_.request.getresponse()

欢迎任何帮助。

使用multiprocessing.Queue的工作示例:

import httplib
import ssl
import copy_reg
from multiprocessing.reduction import rebuild_socket, reduce_socket
from multiprocessing import Queue
import os


def save_sslcontext(obj):
    return obj.__class__, (obj.protocol,)

copy_reg.pickle(ssl.SSLSocket, reduce_socket, rebuild_socket)
copy_reg.pickle(ssl.SSLContext, save_sslcontext)


def parent():
    conn = httplib.HTTPSConnection('www.baidu.com')
    conn.connect()
    queue.put(conn)
    os.wait()


def child():
    conn = queue.get()
    conn.request('GET', '/')
    r = conn.getresponse()
    print r.status


queue = Queue()
if 0 == os.fork():
    child()
else:
    parent()