简体   繁体   English

使用Python套接字的意外HTML错误响应

[英]Unexpected HTML Error Response using Python Sockets

I am following a tutorial to retrieve the HTML from a webpage using Python Sockets found here . 我正在遵循一个教程,该教程使用此处找到的Python套接字从网页检索HTML。

I have an Apache server running on an Ubuntu guest that is hosting a single HTML file for my website. 我有一个在Ubuntu来宾上运行的Apache服务器,该来宾为我的网站托管一个HTML文件。 I have made a DNS entry on my host OS's /etc/hosts file to make the webpage accessible with the url vulnerable . 我已经在主机操作系统的/etc/hosts文件中创建了DNS条目,以使该网页可访问url vulnerable

I have verified that my webpage can be accessed from a web browser on my host machine. 我已验证可以从主机上的Web浏览器访问网页。

I have made a few modifications to the code to fit my case. 我对代码做了一些修改以适合我的情况。

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)

When I try to retrieve the HTML from my website, I get an unexpected Error 404. 当我尝试从网站检索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>

I do not understand why I am getting this 404 error when I can reach my webpage from a web browser without issue. 我不明白为什么可以从Web浏览器访问网页时没有出现此404错误的原因。

Here's your problem 这是你的问题

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

You need to specify the resource you want to retrieve -- that's why you're receiving The requested URL /HTTP/1.1 was not found on this server as a response from the web server. 您需要指定要检索的资源-这就是为什么您要接收的原因The requested URL /HTTP/1.1 was not found on this server作为来自Web服务器的响应。 You're requesting the resource /HTTP/1.1 , which is not found and results in the 404 response. 您正在请求/HTTP/1.1资源,该资源未找到并导致404响应。

In message make sure you specify the resource you want to retrieve, like message确保您指定要检索的资源,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM