python - Invalid response when I use my socket code
问题描述
I have a socket test file, and running:
import socket
sock = socket.socket()
sock.bind(('127.0.0.1', 8080))
sock.listen(5)
while True:
conn, addr = sock.accept()
data = conn.recv(8096)
print(data)
conn.send(b'123456')
conn.close()
when I use browser request the 127.0.0.1:8080
, but in my browser there only get invalid response error:
解决方案
没有响应头,你应该添加响应头:
conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
conn.send(b'123456')
推荐阅读
- android - 如何在不同的 Android 应用程序之间共享密钥(敏感信息)?
- node.js - 使用 jsonix 将 JSON 编组为 XML 会导致 XML 无效
- sql - 如何将2列转置为行
- tensorflow - 如何清除 Tensorflow-Keras GPU 内存?
- rust - 是否可以编写一个折叠迭代器的 const 函数?
- python-3.x - 选择带有 Selenium -error 的复选框:元素无法滚动到视图中
- javascript - 为什么我的`` 有时能工作而其他人不能工作?
- python - Django - 如何为可重用应用程序进行迁移
- java - JAVA企业版的JRE和JVM一样吗?
- .net - 重新执行错误:退出状态1:输出:ProcessUtilityVMImage \UtilityVM:系统找不到指定的路径