S
S
Sergey Grigorov2018-07-13 21:01:57
Python
Sergey Grigorov, 2018-07-13 21:01:57

How to work with raw sockets from Js?

Hello. There is a certain socket server written in Python (pseudocode example):

import json, socket

addr = ("", 8080)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(addr)
while True:
    server.listen(1)
    c_addr, c_sock = server.accept()
    newThread = ThreadingAPI.API(c_addr, c_sock)
    newThread.start()

Question:
How to work with this kind of sockets from JavaScript? Does the JavaScript WebSocket library support this kind of connection between a Python socket server and a JS client?
PS Since the strings are of different lengths, they are sent character-by-character, and already on the client it is assembled character-by-character into a string. I think now to use a more normal approach "in two passes" - the first to send the length of the expected message, the second - the message itself.
But the question concerns exactly how JS works with raw python sockets.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2018-07-13
@sim3x

Use
https://pypi.org/project/websockets/

A
Alexander Aksentiev, 2018-07-13
@Sanasol

socket is not a websocket and has nothing to do with it.
socket is such a raw socket server that you can write anything on it, but out of the box there is nothing to do with it if you do not know what it is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question