尝试将我的socketio客户机从js移植到python,但它不起作用

2024-10-05 12:27:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的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中所拥有的,在我看来,它是一个精确的端口,但不起作用:

^{pr2}$

它看起来是等效的,但显然不是这样,因为代码无法通过下面打开插座.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

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


Tags: iodebugtestcomhttpnewsocketconnection
1条回答
网友
1楼 · 发布于 2024-10-05 12:27:52

如果您查看SocketIO

class SocketIO(EngineIO):

它继承自EngineIO,后者的__init__

^{pr2}$

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)

相关问题 更多 >

    热门问题