I am trying to write a code that gets the html code from a website that the user enters. I am required to write this without using urllib or other libraries of that sort.
from socket import *
url = (input("Please enter url: "))
host=gethostbyname(url)
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((host,80))
clientSocket.send(("GET " + host + "HTTP/1.1\n\n").encode("UTF-8"))
file = clientSocket.recv(1024)
print("The html code: ", file.decode("UTF-8"))
clientSocket.close()
The code runs fine. However, when I input a website such as "www.stackoverflow.com" I get a "bad request" response from the host:
The html code: HTTP/1.1 400 Bad Request
Date: Wed, 23 Mar 2016 16:14:27 GMT
Content-Type: text/html
Content-Length: 177
Connection: close
Server: -nginx
CF-RAY: -
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare-nginx</center>
</body>
</html>
What would be the correct request in order to get the actual html code from the server. Thank you