将py2neo v3与google app引擎配合使用

时间:2016-08-08 13:23:49

标签: python google-app-engine ssl neo4j py2neo

我正在尝试使用谷歌应用引擎上的py2neo设置后端。在推送应用引擎上的dev时,它工作正常,但不幸的是,当我在localhost上使用它时,它不起作用。

首先,我在python中设置了HOME环境变量(感谢这个提示,我的代码可以在我的dev上运行)但是它没有解决localhost问题

然后,我遵循了这个建议"ImportError: No module named _ssl" with dev_appserver.py from Google App Engine 它可以防止一个例外,但另一个例外后会上升。

这是我的追溯

    ft1.1: Traceback (most recent call last):
  File "/Users/Arnaud/Documents/project/app/test/neo4j/test_graph_handler.py", line 13, in test_get_direct_neighbours
    selection = self.graph_handler.get_direct_neighbours("8")
  File "/Users/Arnaud/Documents/project/app/neo4j/graph_handler.py", line 20, in get_direct_neighbours
    labels(l) AS `relationship`" % self.protect(ean))
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/database/__init__.py", line 694, in run
    return self.begin(autocommit=True).run(statement, parameters, **kwparameters)
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/database/__init__.py", line 370, in begin
    return self.transaction_class(self, autocommit)
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/database/__init__.py", line 1212, in __init__
    self.session = driver.session()
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/packages/neo4j/v1/session.py", line 126, in session
    connection = connect(self.address, self.ssl_context, **self.config)
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/packages/neo4j/v1/bolt.py", line 444, in connect
    if not store.match_or_trust(host, der_encoded_server_certificate):
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/packages/neo4j/v1/bolt.py", line 397, in match_or_trust
    f_out = os_open(self.path, O_CREAT | O_APPEND | O_WRONLY, 0o600)  # TODO: Windows
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/stubs.py", line 73, in fake_open
    raise OSError(errno.EROFS, 'Read-only file system', filename)
OSError: [Errno 30] Read-only file system: '/Users/Arnaud/.neo4j/known_hosts'

由于所有内容都在沙盒环境和虚假用户中发布,因此异常是正常的,但不会在开发者身上发生。

这是/Users/Arnaud/Documents/project/app/neo4j/graph_handler.py:20

 13     def get_direct_neighbours(self, ean):
 14         selection = self.graph.run("\
 15                 MATCH\
 16                 (:Product {ean: '%s'})-->(l)<--(n:Product)\
 17                 RETURN\
 18                 n.ean AS `ean`,\
 19                 n.name AS `name`,\
 20                 labels(l) AS `relationship`" % self.protect(ean))
 21         return selection

因此要理解为什么它可以在dev上运行,我尝试本地化沙箱和开发执行开始不同的地方。它就在这里 /Users/Arnaud/Documents/project/app/libs/py2neo/packages/neo4j/v1/bolt.py:427

427     if ssl_context and SSL_AVAILABLE:
428         host, port = host_port
429         if __debug__: log_info("~~ [SECURE] %s", host)
430         try:
431             s = ssl_context.wrap_socket(s, server_hostname=host if HAS_SNI else None)
432         except SSLError as cause:
433             error = ProtocolError("Cannot establish secure connection; %s" % cause.args[1])
434             error.__cause__ = cause
435             raise error
436         else:
437             # Check that the server provides a certificate
438             der_encoded_server_certificate = s.getpeercert(binary_form=True)
439             if der_encoded_server_certificate is None:
440                 raise ProtocolError("When using a secure socket, the server should always provide a certificate")
441             trust = config.get("trust", TRUST_DEFAULT)
442             if trust == TRUST_ON_FIRST_USE:
443                 store = PersonalCertificateStore()
444                 if not store.match_or_trust(host, der_encoded_server_certificate):
445                     raise ProtocolError("Server certificate does not match known certificate for %r; check "
446                                         "details in file %r" % (host, KNOWN_HOSTS))
447     else:
448         der_encoded_server_certificate = None

因为在dev中,ssl_compat.py中的ssl加载在localhost上失败,所以它成功,因此代码进入if语句并在第444行失败

要理解我强迫SSL_AVAILABLE是假的,但即便如此,我还有关于未被识别为套接字的app引擎套接字的奇怪问题

ft1.1: Traceback (most recent call last):
  File "/Users/Arnaud/Documents/project/app/test/neo4j/test_graph_handler.py", line 13, in test_get_direct_neighbours
    selection = self.graph_handler.get_direct_neighbours("8")
  File "/Users/Arnaud/Documents/project/app/neo4j/graph_handler.py", line 20, in get_direct_neighbours
    labels(l) AS `relationship`" % self.protect(ean))
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/database/__init__.py", line 694, in run
    return self.begin(autocommit=True).run(statement, parameters, **kwparameters)
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/database/__init__.py", line 370, in begin
    return self.transaction_class(self, autocommit)
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/database/__init__.py", line 1212, in __init__
    self.session = driver.session()
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/packages/neo4j/v1/session.py", line 126, in session
    connection = connect(self.address, self.ssl_context, **self.config)
  File "/Users/Arnaud/Documents/project/app/libs/py2neo/packages/neo4j/v1/bolt.py", line 460, in connect
    ready_to_read, _, _ = select((s,), (), (), 0)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/remote_socket/_remote_socket.py", line 483, in select
    _SetState(request, _GetSocket(value), POLLIN)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/remote_socket/_remote_socket.py", line 425, in _GetSocket
    raise ValueError('select only supported on socket objects.')
ValueError: select only supported on socket objects.

如果有人遇到同样的问题,我会对此感兴趣,因为在任何测试之前必须推动开发人员非常痛苦。

修改 对于那些想要知道的人,因为我还没有来自nigel的回答而且我需要一些快速的解决方案,我已经创建了自己的类来发送和接收密码请求,它与app引擎兼容并且我把它推到了要点:https://gist.github.com/ArnaudParan/e26f291ba8b3c08e5b762d549667c7d6这是实验性的,如果要求完整节点可能不起作用,但如果它可以提供帮助,我会发布它

1 个答案:

答案 0 :(得分:0)

第一个回溯可能表明py2neo包可能与GAE's sandbox restrictions不兼容。特别是它显示尝试以写入模式(/Users/Arnaud/.neo4j/known_hosts)打开os_open(self.path, O_CREAT | O_APPEND | O_WRONLY, 0o600)文件,这是不允许的。检查是否可以将py2neo配置为不这样做。

沙箱也对套接字有限制,请参阅Limitations and restrictions