我已經寫了一個Python服務器,我想發送HTTP響應消息代碼:400而不是響應「網站即將推出!根據客戶的要求,請告訴我該怎麼做。如何發送HTTP響應消息代碼:400在Python中
服務器代碼是:
import socket
import re
HOST = "localhost"
PORT = 13555
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print ("Serving HTTP on port %s ..." % PORT)
while True:
client_connection, client_address = listen_socket.accept()
request = client_connection.recv(2048)
response = "Website Coming Soon!" #this response should be http response message code:400
http_response = "HTTP/1.1 200 OK\n"+"Content-Type: text/html\n"+"\n"+"<html><body>"+response+"</body></html>\n"
client_connection.sendall(http_response)
client_connection.close()
你需要發送'HTTP/1.1 400 Bad Request'而不是'HTTP/1.1 200 OK'我沒有看到你發送內容的時間。 –
現在我正在使用http_response =「HTTP/1.1 400錯誤的請求\ n」+「Content-Type:text/html \ n」但瀏覽器沒有顯示任何內容 –