App Engine套接字无效参数

时间:2015-11-20 09:17:46

标签: python sockets google-app-engine

我正在使用PyAPNS从Python连接到APN。当作为独立脚本运行时,一切正常并且推送消息得以传递。在Google App Engine开发环境中运行时,出现以下错误:

Traceback (most recent call last):
  File "/usr/local/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/usr/local/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/usr/local/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/usr/local/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/usr/local/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/usr/local/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/mnt/dev/OmegaHandler.py", line 173, in apns
    apns.gateway_server.send_notification("70f23022d76aae0176844087c97f0ff068dd3e3686dbac086b8f82a630d0196a", payload)
  File "/mnt/dev/apns.py", line 544, in send_notification
    self.write(self._get_notification(token_hex, payload))
  File "/mnt/dev/apns.py", line 273, in write
    return self._connection().write(string)
  File "/mnt/dev/apns.py", line 254, in _connection
    self._connect()
  File "/mnt/dev/apns.py", line 230, in _connect
    self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file)
  File "/usr/local/lib/python2.7/ssl.py", line 911, in wrap_socket
    ciphers=ciphers)
  File "/usr/local/lib/python2.7/ssl.py", line 535, in __init__
    if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
  File "/mnt/dev/rsocket.py", line 225, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 22] Invalid argument

我使用this回答来帮助开发环境中的“权限被拒绝”错误。同样,当独立运行时,使用完全相同的库和代码都可以正常运行。该错误似乎来自使用选项getsockopt调用的(1, 3)。我再也不知道了。

为什么会出现在开发环境中,使用库存Python socket.py进行修补,而不是在独立脚本中进行修补。请不要回答我需要启用计费才能使用Socket API,这只发生在开发环境中。感谢。

编辑: 这也发生在不同的计算机上,并且使用不同的服务器(常规SSL套接字连接到Google IP地址范围之外的某个服务器)。

1 个答案:

答案 0 :(得分:7)

不确定这是否仍然有用,或者您是否在其他地方找到了答案。

我有一个非常类似的问题,试图在相当标准的应用引擎代码中使用requests库。

tl; dr - App Engine补丁/提供沙盒socketssl模块在​​自己导入时从socket的命名空间中快照对象。如果socket(和_socket)在导入ssl时不是正确的版本,ssl最终会以SOL_SOCKET为基础的GAE-patch / sandbox值和SO_TYPE(可能不正确),而不是应该来自基本系统安装。如果您确保socket(和_socket)的副本正确无误,ssl将有效。

我向Google提交了一张支持票,并推迟实施this

对于我来说实际上交换物理socket.py实施并不是那么微不足道,所以我走了this solution instead的路线。

这让我大部分都在那里,但问题仍然因为SSL而出现。

在App Engine开发沙箱(即dev_appserver.py)中发出HTTP请求时,我测试了与以下代码几乎完全相同的内容,以使SSL使用原始套接字(而不是URLFetch):

import os

def _patch_ssl_support():

    # Only patch SSL support if it's local dev.
    if not os.environ.get('SERVER_SOFTWARE', '').startswith('Development'):
        return

    import imp
    import inspect
    from google.appengine.tools.devappserver2.python import sandbox

    # Allow the sandbox to read _ssl and _socket.
    sandbox._WHITE_LIST_C_MODULES += ['_socket', '_ssl']

    # Use the system socket.
    # I used inspect here, but many core modules should work. 
    # It ultimately depends on your python installation.
    runtime_path = os.path.realpath(inspect.getsourcefile(inspect))
    runtime_dir = os.path.dirname(runtime_path)

    # Patch and reload the socket module implementation.
    system_socket = os.path.join(runtime_dir, 'socket.py')
    imp.load_source('socket', system_socket)

    # Patch and reload the ssl module implementation.
    system_ssl = os.path.join(runtime_dir, 'ssl.py')
    imp.load_source('ssl', system_ssl)

    # Patch and/or reload any other libraries you suspect may have copied values
    #   from the socket or ssl namespaces.

# Patch SSL support before you do anything else.
_patch_ssl_support()

import webapp2

# Setup app engine application, or something that runs in an app engine runtime:
app = webapp2.WSGIApplication(routes)