gpt4 book ai didi

python - 尝试从低级套接字服务器发送 HTTP 响应

转载 作者:可可西里 更新时间:2023-11-01 15:30:24 26 4
gpt4 key购买 nike

这是我的代码:

# Process connections

print('Listening on port', port)
while True:
c, addr = s.accept()
print("Got connection from", addr)
msg = "<html></html>"

response_headers = {
'Content-Type': 'text/html; encoding=utf8',
'Content-Length': len(msg.encode(encoding="utf-8")),
'Connection': 'close',
}

response_headers_raw = ''.join('%s: %s\n' % (k, v) for k, v in response_headers.items())

response_proto = 'HTTP/1.1'
response_status = '200'
response_status_text = 'OK' # this can be random

# sending all this stuff
r = '%s %s %s' % (response_proto, response_status, response_status_text)
c.send(r.encode(encoding="utf-8"))
c.send(response_headers_raw.encode(encoding="utf-8"))
c.send('\n'.encode(encoding="utf-8")) # to separate headers from body
c.send(msg.encode(encoding="utf-8"))

c.close()

当我在浏览器上访问我的本地 ip 时,我收到一个错误页面,上面写着

ERR_CONNECTION_RESET

我做错了什么?

注意:

完整的代码片段在这里:

import socket

# Create socket object and set protocol
s = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)

# Get name of local machine
host = socket.gethostname()
port = 80

# Bind
s.bind((host, port))

# Listen and set backlog (?)
s.listen(5)

# Process connections
print('Listening on port', port)
while True:
c, addr = s.accept()
print("Got connection from", addr)
msg = "<html></html>"

response_headers = {
'Content-Type': 'text/html; encoding=utf8',
'Content-Length': len(msg.encode(encoding="utf-8")),
'Connection': 'close',
}

response_headers_raw = ''.join('%s: %s\n' % (k, v) for k, v in response_headers.items())

response_proto = 'HTTP/1.1'
response_status = '200'
response_status_text = 'OK' # this can be random

# sending all this stuff
r = '%s %s %s' % (response_proto, response_status, response_status_text)
c.send(r.encode(encoding="utf-8"))
c.send(response_headers_raw.encode(encoding="utf-8"))
c.send('\n'.encode(encoding="utf-8")) # to separate headers from body
c.send(msg.encode(encoding="utf-8"))

c.close()

最佳答案

问题是 HTTP header 需要用 \r\n 进行换行,而不仅仅是 \n。参见 RFC 1945 - HTTP/1.0 Sec 2.2RFC 7230 - HTTP/1.1 Syntax and Routing Sec 3 .

如果您更改它,它将解决问题。

import socket

# Create socket object and set protocol
s = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)

# Get name of local machine
host = "127.0.0.1"
port = 8081

# Bind
s.bind((host, port))

# Listen and set backlog (?)
s.listen(5)

# Process connections
print('Listening on port', port)
while True:
c, addr = s.accept()
print("Got connection from", addr)
msg = "<html><body><h1>This is a test</h1><p>More content here</p></body></html>"

response_headers = {
'Content-Type': 'text/html; encoding=utf8',
'Content-Length': len(msg),
'Connection': 'close',
}

response_headers_raw = ''.join('%s: %s\r\n' % (k, v) for k, v in response_headers.items())

response_proto = 'HTTP/1.1'
response_status = '200'
response_status_text = 'OK' # this can be random

# sending all this stuff
r = '%s %s %s\r\n' % (response_proto, response_status, response_status_text)
c.send(r)
c.send(response_headers_raw)
c.send('\r\n') # to separate headers from body
c.send(msg.encode(encoding="utf-8"))

c.close()

关于python - 尝试从低级套接字服务器发送 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36122461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com