我剛剛在 python3 中設定了一個小型 TCP-socket 服務器,它等待連接,發送一個小的編碼文本并等待來自客戶端的資料。
Python代碼:
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()
最后,應該有一個瀏覽器界面,它可以使用 JQuery 與 python 后端進行通信,或者只是接收和發送(使用資料!)套接字的東西。
謝謝 :D
編輯:
忘了說我可以在瀏覽器中接收文本,我只需要知道如何發回資料。
uj5u.com熱心網友回復:
如果您需要連接到 NodeJS javascript 環境,那么您應該看看這個原生包 https://nodejs.org/api/net.html#class-netsocket
如果您需要連接到瀏覽器 javascript 環境,那么您應該看看 WebSockets
- Python:https ://websockets.readthedocs.io/en/stable/#:~:text=websockets is a library for,an elegant coroutine-based API 。
- Javascript https://developer.mozilla.org/es/docs/Web/API/WebSockets_API
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/412093.html
標籤:
上一篇:我可以在GoogleSheetsQUERY中使用SQLCASE陳述句嗎?
下一篇:Multiprocessing FlaskOSError:[Errno48]Addressalreadyinuse/OSError:[Errno38]非套接字上的套接字操作
