1

I am following a tutorial to retrieve the HTML from a webpage using Python Sockets found here.

I have an Apache server running on an Ubuntu guest that is hosting a single HTML file for my website. I have made a DNS entry on my host OS's /etc/hosts file to make the webpage accessible with the url vulnerable.

I have verified that my webpage can be accessed from a web browser on my host machine.

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.

<!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.

1 Answer 1

1

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. You're requesting the resource /HTTP/1.1, which is not found and results in the 404 response.

In message make sure you specify the resource you want to retrieve, like

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

Sign up to request clarification or add additional context in comments.

1 Comment

This was it! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.