程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PyQt5 websocket 和 NodeJS 服务器之间的身份验证大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决PyQt5 websocket 和 NodeJS 服务器之间的身份验证?

开发过程中遇到PyQt5 websocket 和 NodeJS 服务器之间的身份验证的问题如何解决?下面主要结合日常开发的经验,给出你关于PyQt5 websocket 和 NodeJS 服务器之间的身份验证的解决方法建议,希望对你解决PyQt5 websocket 和 NodeJS 服务器之间的身份验证有所启发或帮助;

我正在尝试使用 QtWebSockets 模块通过 PyQt5 应用程序验证与节点服务器的 websocket 连接。我已经成功地让他们说话了。从我读过的内容来看,在服务器端,我应该对来自客户端的“升级”请求进行身份验证。但是,我无法找到示例或推断出如何执行此操作。服务器部分对我来说不是很清楚,PyQt 客户端部分更不明白。

最终目标是让服务器从不同的程序获取发送的数据(带有一些 ID),并将数据传递给正确的客户端(基于此 ID,与客户端凭据相关联)。客户端程序会处理数据(图表、计算)。

目前,客户端程序要求提供用户凭据(并且对它们不做任何处理),并显示“正在连接”消息。打开连接时,消息消失,并且在 UI 中选中“服务器已连接”复选框。我接下来希望完成的是尝试与用户凭据建立连接,并在服务器端使用这些凭据来标识客户端,从而为正确的连接提供正确的数据。

“主”类

class AppDemo(QWidget):
    def  __init__(self):
        super().__init__()
        uic.loadUi('serverConnection.ui',self)
        self.initUiSetTings()
        self.username = ""
        self.password = ""
        self.askCredentials()
        clIEnt = ClIEnt(self) # here we Could pass username and password gotten from user

    def initUiSetTings(self):
        ...

    def checkCBox(parent,Box,bool):
        ...

    def askCredentials(self):
        ... # get username and password gotten from the user

Websocket 客户端类。

class ClIEnt(QtCore.QObject):
    def __init__(self,parent):
        super(ClIEnt,self).__init__(parent)
        self.clIEnt =  QtWebSockets.QWebSocket("",QtWebSockets.QWebSocketProtocol.Version13,NonE)

        self.clIEnt.error.connect(self.error)

        self.clIEnt.open(QtCore.QUrl(url))

        self.clIEnt.pong.connect(self.onPong)
        self.clIEnt.disconnected.connect(self.ondisconnect)
        self.clIEnt.connected.connect(self.onConnect)
        self.clIEnt.textmessageReceived.connect(self.onmessagE)
        
        #QtCore.QTimer.singleShot(2000,self.do_Ping)
        


    def onConnect(self):
        print("Connected to server")
        self.setConnectioncheckBox(true)
        self.parent().connecTing.hIDe()
        connected = True

    def ondisconnect(self):
        print("Connection disconnected")
        connected = false
        self.setConnectioncheckBox(false)

    def do_Ping(self):
        print("clIEnt: do_Ping")
        self.clIEnt.Ping(b"foo")

    def send_message(self):
        print("clIEnt: send_message")
        self.clIEnt.sendTextmessage("asd")

    def onmessage(self,payload):
         print(payload)


    def setConnectioncheckBox(self,bool):
        self.parent().checkCBox(self.parent().ch_check,bool)

    def onPong(self,elapsedtime,payload):
        print("onPong - time: {} ; payload: {}".format(elapsedtime,payload))
        connected = True

    def error(self,error_codE):
        print("error code: {}".format(error_codE))
        print(self.clIEnt.errorString())
        connected = false
        self.setConnectioncheckBox(false)
        self.attemptReconnect()

    def close(self):
        self.clIEnt.close()
        connected = false
        self.setConnectioncheckBox(false)

    def attemptReconnect(self):
        QtCore.QTimer().start(5000)
        print("AttempTing reconnection..")
        self.clIEnt.open(QtCore.QUrl("ws://127.0.0.1:3210"))
        QtCore.QTimer().stop()

服务器代码,几乎是从示例中粘贴过来的:

const path = require("path");
const clIEnt = require(path.join(__dirname,'ClIEnt.Js'));
const http = require('http');
const WebSocketServer = require('websocket').server;
const server = http.createServer();
const port = 3210;

var clIEnt = [];

server.Listen(port);
const wsServer = new WebSocketServer({
    httpServer: server
});

console.log(`Listening on ${port}`)

wsServer.on('connect',function(connection){
    console.log("ClIEnt connected");
    connection.on('message',function message(msg) {
        console.log(`Received message ${msg} from user ${ClIEnt}`);
      });
});

server.on('upgrade',function upgrade(request,socket,head) {
    // This function is not defined on purpose. Implement it with your own logic.
    console.log("ws");
    // authenticate(request,(err,clIEnt) => {
    //   if (err || !clIEnt) {
    //     socket.write('http/1.1 401 Unauthorized\r\n\r\n');
    //     socket.destroy();
    //     return;
    //   }
  
    //   wss.handleUpgrade(request,head,function done(ws) {
    //     wss.emit('connection',ws,request,clIEnt);
    //   });
    // });
  });
  

wsServer.on('request',function(request) {
    

    const connection = request.accept(null,request.origin);

    connection.on('message',function(messagE) {
      console.log('Received message:',message.utf8Data);
      connection.sendUTF('Hi this is WebSocket server!');
    });
    connection.on('close',function(reasonCode,description) {
        console.log('ClIEnt has disconnected.');
    });
    
});

我知道可以使用标头完成基本身份验证,但我不知道如何在此处实现它或如何实现任何其他身份验证。我在服务器代码中包含了一个注释掉的示例模板,用于在 Node 示例中找到的身份验证。我知道我也应该使用 wss 而不是 ws。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的PyQt5 websocket 和 NodeJS 服务器之间的身份验证全部内容,希望文章能够帮你解决PyQt5 websocket 和 NodeJS 服务器之间的身份验证所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。