Python socketIO_client连接到Heroku应用程序(在localhost中可以正常工作)

时间:2018-11-29 08:29:16

标签: python node.js heroku

我已经在Google和这些论坛上闲逛了一段时间,但似乎找不到解决方法...

我成功地创建了一个可以在Heroku上运行的socket.io应用程序(是的!),并且一切正常,我认为我不需要将源代码发布到实际的应用程序上,因为它可以完美地在多个应用程序上运行计算机等...,它只是通过使用node.js制作socket.io应用程序的标准方式进行设置,并且效果很好...

问题:我正尝试使用python在客户端本地连接到此应用。

为此,我尝试使用简单的socketIO_client库,但是仅基于他们的文档,我就遇到了麻烦,因此在这些论坛上,我找到了以下代码并将其放在自己的程序包中:

...    partial_ratio
...    78
...    83
...    100
...    100
...    100

然后在我的主要python文件中使用它:

import logging
import socketIO_client
from socketIO_client.transports import get_response
from socketIO_client.parsers import get_byte, _read_packet_text, parse_packet_text

from requests.exceptions import ConnectionError
def _new_read_packet_length(content, content_index):
    packet_length_string = ''
    while get_byte(content, content_index) != ord(':'):
        byte = get_byte(content, content_index)
        packet_length_string += chr(byte)
        content_index += 1
    content_index += 1
    try:
        return content_index, int(packet_length_string)
    except Exception as ok:
        print ok

def new_decode_engineIO_content(content):
    content_index = 0
    content_length = len(content)
    while content_index < content_length:
        try:
            content_index, packet_length = _new_read_packet_length(
                content, content_index)
        except IndexError:
            break
        content_index, packet_text = _read_packet_text(
            content, content_index, packet_length)
        engineIO_packet_type, engineIO_packet_data = parse_packet_text(
            packet_text)
        yield engineIO_packet_type, engineIO_packet_data

def new_recv_packet(self):
    params = dict(self._params)
    params['t'] = self._get_timestamp()
    response = get_response(
        self.http_session.get,
        self._http_url,
        params=params,
        **self._kw_get)
    for engineIO_packet in new_decode_engineIO_content(response.content):
        engineIO_packet_type, engineIO_packet_data = engineIO_packet
        yield engineIO_packet_type, engineIO_packet_data
def CobSocket(url, port):
    return socketIO_client.SocketIO(url, port)

setattr(socketIO_client.transports.XHR_PollingTransport, 'recv_packet', new_recv_packet)
logging.basicConfig(level=logging.DEBUG)

因此您可以看到我正在将heroku应用程序URL插入套接字连接(如果我根本不使用heroku就使用localhost,则顺便说一句,上面的脚本实际上可以正常工作,它确实可以很好地连接到node.js套接字服务器,问题仅在于它已上传到Heroku)。

在终端上运行时,我从在终端上运行import cobsockit try: socket = cobsockit.CobSocket("insta-menorah.herokuapp.com/", 39203) socket.emit('welcome', "a new connection is here") socket.wait(seconds=1) except cobsockit.ConnectionError: print "something didn't work out too well" 获得的端口号。我还听说,如果要从外部访问它,则需要使用端口80,但是当我这样做时,在终端中出现以下错误:

heroku run printenv -a insta-menorah

所以,我什至没有收到任何错误消息,但是当我运行脚本时,它似乎永远死了,什么也没做,这就是我运行的内容,并且卡住了一段时间:

Starting new HTTP connection (1): insta-menorah.herokuapp.com:80
DEBUG:urllib3.connectionpool:http://insta-menorah.herokuapp.com:80 "GET //socket.io/?EIO=3&transport=polling&t=1543480624168-0 HTTP/1.1" 200 None
invalid literal for int() with base 10: '<html>\n    <head>\n\n    </head>\n    <body>\n        <script src="/socket.io/socket.io.js"></script>\n        <script>\n        var server = io.connect(window.location.hostname);\n        server.on("dude lo'
Traceback (most recent call last):
  File "socketpython.py", line 8, in <module>
    socket = cobsockit.CobSocket("http://insta-menorah.herokuapp.com/", 80)
  File "/Users/admin/Documents/menorah/cobsockit.py", line 46, in CobSocket
    return socketIO_client.SocketIO(url, port)
  File "/usr/local/lib/python2.7/site-packages/socketIO_client/__init__.py", line 353, in __init__
    resource, hurry_interval_in_seconds, **kw)
  File "/usr/local/lib/python2.7/site-packages/socketIO_client/__init__.py", line 54, in __init__
    self._transport
  File "/usr/local/lib/python2.7/site-packages/socketIO_client/__init__.py", line 62, in _transport
    self._engineIO_session = self._get_engineIO_session()
  File "/usr/local/lib/python2.7/site-packages/socketIO_client/__init__.py", line 76, in _get_engineIO_session
    transport.recv_packet())
  File "/Users/admin/Documents/menorah/cobsockit.py", line 42, in new_recv_packet
    for engineIO_packet in new_decode_engineIO_content(response.content):
  File "/Users/admin/Documents/menorah/cobsockit.py", line 25, in new_decode_engineIO_content
    content, content_index)
TypeError: 'NoneType' object is not iterable

然后大约10到15分钟后,它显示如下:

python socketpython.py
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): insta-menorah.herokuapp.com:39203

,然后重新开始连接。...

SO:我怎么能简单地使用Python连接到Heroku node.js应用!!

0 个答案:

没有答案