Python & Nameko - GreenSSLSocket 没有 public 构造函数。 SSLContext.wrap_socket() 返回实例。

Python & Nameko - GreenSSLSocket does not have a public constructor. Instances are returned by SSLContext.wrap_socket().

我创建了一个 nameko 服务,可以向提供的 phone 号码发送短信。使用 Africas Talking API。该服务运行成功,但一旦您使用 nameko shell 它就不起作用了。但是 phone 号码已成功传递到服务中。

滑子 Shell:

 >>> n.rpc.sms.text('+254712619789')
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/nameko/rpc.py", line 369, in __call__
        return reply.result()
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/nameko/rpc.py", line 327, in result
        raise deserialize(error)
    nameko.exceptions.RemoteError: TypeError GreenSSLSocket does not have a public constructor. Instances are returned by SSLContext.wrap_socket().

滑子服务

nameko run at
starting services: sms
Connected to amqp://guest:**@127.0.0.1:5672//
+254712619789
error handling worker <WorkerContext [sms.text] at 0x10414a128>: GreenSSLSocket does not have a public constructor. Instances are returned by SSLContext.wrap_socket().
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/nameko/containers.py", line 391, in _run_worker
    result = method(*worker_ctx.args, **worker_ctx.kwargs)
  File "./at.py", line 23, in text
    response = sms.send("Hello Message!", [phone_no])
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/africastalking/SMS.py", line 32, in send
    return self._make_request(url, 'POST', headers=self._headers, params=None, data=data, callback=callback)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/africastalking/Service.py", line 92, in _make_request
    res = self.__make_post_request(url=url, headers=headers, data=data, params=params)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/africastalking/Service.py", line 78, in __make_post_request
    data=data,
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/adapters.py", line 445, in send
    timeout=timeout
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 849, in _validate_conn
    conn.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connection.py", line 356, in connect
    ssl_context=context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 359, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eventlet/green/ssl.py", line 402, in wrap_socket
    return GreenSSLSocket(sock, *a, _context=self, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eventlet/green/ssl.py", line 68, in __init__
    ca_certs, do_handshake_on_connect and six.PY2, *args, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 788, in __init__
    f"{self.__class__.__name__} does not have a public "
TypeError: GreenSSLSocket does not have a public constructor. Instances are returned by SSLContext.wrap_socket().

at.py

import json
from nameko.web.handlers import http
from nameko.rpc import rpc
import africastalking

class HttpService(object):
    name = "sms"
    @rpc
    def text(self, phone_no):
        print (phone_no)
        africastalking.initialize(username, api_key)
        # Initialize a service e.g. SMS
        sms = africastalking.SMS
        # Use the service synchronously
        response = sms.send("Hello Message!", [phone_no])
        print(response)
        return response

问题可能是 eventlet 与 python 3.7 不兼容。你能试试 python 3.6 吗?

(参见 https://github.com/eventlet/eventlet/issues/502