【问题标题】:Unexpected HTML Error Response using Python Sockets使用 Python 套接字的意外 HTML 错误响应
【发布时间】: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


    【解决方案1】:

    这是你的问题

    message = "GET /HTTP/1.1\r\n\r\n".encode('utf-8')

    您需要指定要检索的资源——这就是您收到The requested URL /HTTP/1.1 was not found on this server 作为来自网络服务器的响应的原因。您正在请求资源 /HTTP/1.1,该资源未找到并导致 404 响应。

    message 中确保指定要检索的资源,例如

    message = "GET /index.html HTTP/1.1\r\n\r\n".encode('utf-8')

    【讨论】:

    • 就是这样!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2018-02-14
    • 2014-03-31
    • 2023-03-23
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多