【发布时间】:2016-01-22 10:55:31
【问题描述】:
我正在学习使用 Python 套接字从网页中检索 HTML 的教程,找到了 here。
我有一个运行在 Ubuntu 客户机上的 Apache 服务器,它为我的网站托管一个 HTML 文件。我在主机操作系统的 /etc/hosts 文件上创建了一个 DNS 条目,以使网页可以通过 url vulnerable 访问。
我已验证可以从主机上的网络浏览器访问我的网页。
我对代码做了一些修改以适应我的情况。
import socket
import sys # needed for sys.exit()
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print ("Failed to initialize socket")
sys.exit()
print ("Socket initialized")
host = "vulnerable"
port = 80
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror as e:
print ("Hostname could not be resolved. Exiting")
sys.exit()
s.connect((remote_ip, port))
print ("Socket Connected to " +host+ " on IP " + remote_ip)
message = "GET /HTTP/1.1\r\n\r\n".encode('utf-8') # convert string to byte message, otherwise won't send
try:
s.sendall(message)
except socket.error:
print ("Send Failed")
sys.exit()
print ("Message sent successfully")
reply = s.recv(4096)
print (reply)
当我尝试从我的网站检索 HTML 时,我收到了意外的错误 404。
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /HTTP/1.1 was not found on this server.</p>
<hr>
<address>Apache/2.4.10 (Ubuntu) Server at 127.0.1.1 Port 80</address>
</body></html>
当我可以从网络浏览器毫无问题地访问我的网页时,我不明白为什么会收到此 404 错误。
【问题讨论】:
标签: python html apache sockets http-status-code-404