Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Communication between python and JavaScript

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to achieve python and JavaScript communication". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve python and JavaScript communication" can help you solve your doubts.

Js and python are two languages. Js handles web data, python can be developed as a server, and the two communicate through websocket.

Websocket is the encapsulation of socket, which saves the trouble of HTTP protocol conversion in communication.

Torando/Djiango platform is recommended for large and medium-sized projects. The two platforms are highly integrated and convenient for development and use.

The experiment takes sending pictures from the html page to the back end of the server as an example.

After receiving the picture, the server opens the service with local port 10086 and waits for the front-end connection. The service stores the image url sent by the front end in the local file car.bmp.

Import asyncioimport websocketsimport urllib.request async def recv_user_msg (websocket): while True: url = await websocket.recv () urllib.request.urlretrieve (url,'car.bmp') await websocket.send ('ok') async def run (websocket, path): while True: try: await recv_user_msg (websocket) except websockets.ConnectionClosed: print ("ConnectionClosed..." Path) break if _ _ name__ = ='_ _ main__': print ("127.0.0.1 name__ 10086 websocket...") Asyncio.get_event_loop () .run_until_complete (websockets.serve (run, "127.0.0.1", 10086)) asyncio.get_event_loop () .run_forever ()

Lack of module direct pip install installation solution.

Pip install websocketspip install urllib

Embed the img tag on the page, connect the server, click send picture, send the picture to the server, and display the return result.

Test Socket--ws://127.0.0.1:10086

Send the picture var socket; var ws = new WebSocket ("ws://127.0.0.1:10086/test"); socket = ws; ws.onopen = function () {console.log ('connected successfully');}; ws.onmessage = function (evt) {var received_msg = evt.data Alert ('recv:' + received_msg +' send complete'); ws.onclose = function () {s = 'disconnected' alert (s); function send_img () {image = document.getElementById ('img'); socket.send (image.src) } after reading this, the article "how to achieve python and JavaScript Communication" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report