python - How to display the information sent by socket in the browser
淡淡烟草味
淡淡烟草味 2017-05-18 10:56:56
0
2
860

The socket server built using the socket module, for example, listens to the local port 4399. I use a browser to connect, but the characters sent from the socket cannot be displayed.
How to do this?

淡淡烟草味
淡淡烟草味

reply all(2)
某草草

Because your port is not 80, the browser cannot use the http protocol to parse your request, because it does not know what 4399 is for, so if you want to use monitoring 4399, and use the browser to see the socket server sending message, you must construct the http message yourself

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    # tcp 报文
s.bind(('localhost', 4399))
s.listen(2)
req, req_info = s.accept()
req.recv(65549)
# http协议头是文本形式, 以\r\n作为每个字段的分隔, 最后头部以\r\n结束, 所以我们主要构造好 http头, 浏览器就能识别的, 接下来的正文, 就能按照html的标准的编写了
req.send('HTTP/1.1 200 OK\r\n\r\n<html><body>hello</body></html>')
Peter_Zhu

Regarding Python SOCKET, if you want the browser to see the data, then you must implement the HTTP protocol. Otherwise, how will the browser know what you are sending and how much data is there?
About Python Socket HTTP, please see here https://github.com/thisforeda...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!