Home > Back-end >  How to communicate between Python3 and JavaScript via sockets
How to communicate between Python3 and JavaScript via sockets

Time:01-17

I just set up a small TCP-socket server in python3, which waits for a connection, sends a small encoded text and waits for data from the client.

Python-Code:

import socket

sock = socket.socket()
sock.bind(("0.0.0.0", 20000))

sock.listen(5)

while True:
    Client, address = sock.accept()
    Client.send("ping".encode())
    print(Client.recv(1024).decode())  # Here I want to receive data from the client
    Client.close()

At the end, there should be a browser interface which can communicate with the python backend with JQuery or just something that receive and send (With data!) sockets.

Thank you :D

EDIT:

Forgot to say that I can receive the text in a browser, I just need to know how to send data back.

CodePudding user response:

if you need to connect to a NodeJS javascript environment then you should take a look at this native package https://nodejs.org/api/net.html#class-netsocket

if you need to connect to a browser javascript environment then you should take a look at WebSockets

  •  Tags:  
  • Related