gpt4 book ai didi

javascript - 试图将我的 socketio 客户端从 js 移植到 python 但它不工作

转载 作者:行者123 更新时间:2023-11-30 20:36:01 24 4
gpt4 key购买 nike

我有以下 JS 代码,非常适合通过 socketio 客户端库连接到套接字:

userApi = 'userapiaccesfskdglk';
userAccess = 'useaccesfjkasdfdlkf2';

var axios = require('axios');
var socket = require('socket.io-client')('wss://test-meownow12345.com:4566');

socket.emit('Authenticate', {api: userApi, access: userAccess});

socket.on('Party', function(party) {
console.log('party', party)

})

这些不是实际使用的 userApi、userAccess 或 url,但使用它们仍然得到相同的意义。

这就是我在 python 中所拥有的,对我来说看起来像是一个确切的端口,但不起作用:

from socketIO_client import SocketIO, LoggingNamespace
import logging
logging.basicConfig(level=logging.DEBUG)


def on_party():
print('connect')

userApi = 'userapiaccesfskdglk'
userAccess = 'useaccesfjkasdfdlkf2'

with SocketIO('wss://test-meownow12345.com', 4566, LoggingNamespace) as socketIO:
socketIO.emit('Authenticate', {'api': userApi, 'access': userAccess})
socketIO.on('Party', on_party)

它似乎是等效的,但显然不是,因为代码无法通过以下打开 socket.io 连接的行:

with SocketIO('wss://test-meownow12345.com', 4566, LoggingNamespace) as socketIO:

在我的控制台中,它打印出以下重复的日志错误:

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): wss
WARNING:socketIO-client:wss:4566//test-meownow12345.com/socket.io [engine.io waiting for connection] HTTPConnectionPool(host='wss', port=4566): Max retries exceeded with url: //test-meownow12345.com/socket.io/?EIO=3&transport=polling&t=1523668232386-0 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x105000278>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))

我不太确定这是什么意思。

我尝试更改给出此消息的行并删除“wss://”部分以使其成为:

with SocketIO('test-meownow12345.com', 4566, LoggingNamespace) as socketIO:

但这仍然失败,尽管在日志中重复了一条新消息:

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): test-meownow12345.com
WARNING:socketIO-client:test-meownow12345.com:4566/socket.io [engine.io waiting for connection] ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
DEBUG:urllib3.connectionpool:Starting new HTTP connection (2): test-meownow12345.com
DEBUG:urllib3.connectionpool:Starting new HTTP connection (3): test-meownow12345.com

非常感谢任何帮助,这是一个非常令人沮丧的问题。

最佳答案

如果你看SocketIO

class SocketIO(EngineIO):

它继承自EngineIO,它有__init__作为

class EngineIO(LoggingMixin):

def __init__(
self, host, port=None, Namespace=EngineIONamespace,
wait_for_connection=True, transports=TRANSPORTS,
resource='engine.io', hurry_interval_in_seconds=1, **kw):
self._is_secure, self._url = parse_host(host, port, resource)
self._wait_for_connection = wait_for_connection

parse_host 的定义是

def parse_host(host, port, resource):
if not host.startswith('http'):
host = 'http://' + host
url_pack = parse_url(host)
is_secure = url_pack.scheme == 'https'
port = port or url_pack.port or (443 if is_secure else 80)
url = '%s:%s%s/%s' % (url_pack.hostname, port, url_pack.path, resource)
return is_secure, url

这表明用法应该如下所示

with SocketIO('https://test-meownow12345.com', 4566, LoggingNamespace) as socketIO:
socketIO.emit('Authenticate', {'api': userApi, 'access': userAccess})
socketIO.on('Party', on_party)

关于javascript - 试图将我的 socketio 客户端从 js 移植到 python 但它不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49826976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com