简体   繁体   English

如何通过 Node.js 将数据发送到 Python SSL 套接字

[英]How to send data to a Python SSL socket via Node.js

Context语境

I have a https server running on node.js that serves the requests of various client applications written in python and I want to enable peer to peer communication between the clients.我有一个 https 服务器在 node.js 上运行,它服务于用 python 编写的各种客户端应用程序的请求,我想启用客户端之间的对等通信。 To achieve this, I'm keeping track of the 'online' clients on the server that then sends the necessary data so that the peers can communicate between themselves.为了实现这一点,我正在跟踪服务器上的“在线”客户端,然后发送必要的数据,以便对等方可以在它们之间进行通信。

My goal:我的目标:

  1. The client applications opens three sockets (one for receiving messages from other clients, one for sending messages to other clients and one for receiving messages from the server - let's call this one notification socket )客户端应用程序打开三个 sockets(一个用于接收来自其他客户端的消息,一个用于向其他客户端发送消息,一个用于接收来自服务器的消息 - 我们称之为一个notification socket
host = '127.0.0.1'

listener_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_listener_socket = ssl.wrap_socket(listener_socket, keyfile= pkey_path, 
    certfile= crt_path, cert_reqs= ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLSv1_2)
ssl_listener_socket.bind((host, 0))
listener_name = ssl_listener_socket.getsockname()

sender_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sender_socket = ssl.wrap_socket(sender_socket, keyfile= pkey_path, 
    certfile= crt_path, cert_reqs= ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLSv1_2)
ssl_sender_socket.bind((host, 0))
sender_name = ssl_sender_socket.getsockname()

notification_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_notification_socket = ssl.wrap_socket(notification_socket, keyfile= pkey_path, 
    certfile= crt_path, cert_reqs= ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLSv1_2)
ssl_notification_socket.bind((host, 0))
notification_name = ssl_notification_socket.getsockname()
  1. The client sends the various socket's ips and ports to the server客户端将各种套接字的 ips 和端口发送到服务器
  2. When the server receives a connection request from a client that wants to talk to another client (clients are identified by usernames), if both of them are "online", the server opens two ssl sockets (one for each client), connects to each client's notification socket and sends it the information about the other client's sockets.当服务器收到一个客户端的连接请求,想要与另一个客户端(客户端由用户名标识)进行通信时,如果两者都是“在线”,则服务器打开两个 ssl sockets(每个客户端一个),连接到每个客户端的notification socket并向其发送有关其他客户端的 sockets 的信息。

The issue问题

I'm not being capable of doing task #3.我无法完成任务 #3。 How can I create two ssl sockets in nodejs and send data to the python sockets?如何在nodejs中创建两个ssl sockets并将数据发送到python sockets?

On a design note, I'd do this differently.在设计说明上,我会以不同的方式做这件事。

  1. Setup a socket server for the client为客户端设置套接字服务器
  2. Each client opens a socket connection with the main server.每个客户端打开一个与主服务器的套接字连接。 Use websockets or python-socketio for this.为此使用 websockets 或 python-socketio。 This is what you want to do I presume?我猜这就是你想要做的?
  3. Clients send their information to the server through the socket and server sends the information to other clients.客户端通过套接字将他们的信息发送到服务器,服务器将信息发送给其他客户端。
  4. Each client receives information about other clients' hosts.每个客户端接收有关其他客户端主机的信息。 You can now choose to create the socket connections to the other servers.您现在可以选择创建与其他服务器的套接字连接。 N for n hosts. N 代表 n 个主机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM