我正在制作一个简单的Web服务器,并且具有python函数来处理请求,如下所示:
def handle_request(client_connection):
request = client_connection.recv(1024)
print(request.decode())
http_response = """\
HTTP/1.1 200 OK\n
Hello, World!
"""
client_connection.sendall(http_response)
现在,每当我在浏览器(客户端)上键入其他内容时,我仍然会收到200 OK响应。如何处理404
响应?
答案 0 :(得分:0)
试试下面的代码:
import requests
# Getting a request: this generates an error
response = requests.get('http://www.google.com.br/error')
if response.ok:
print('This URL is working properly!')
else:
print('This page either does not exist or is out.')
使用 .ok
属性以获得 OK 验证/布尔值。
#和平