Jonathan
Jonathan

Reputation: 133

How to communicate between Python3 and JavaScript via sockets

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.

Upvotes: 0

Views: 1508

Answers (1)

Martin Munilla
Martin Munilla

Reputation: 302

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

Upvotes: 1

Related Questions