for a school assignment I need to send GET requests and receive the data using only sockets. I keep getting an HTTP/1.1 400 Bad Request error, no matter how I try to format the GET request.
Here is my code(please excuse me if it’s terrible, this is my first ever python project):
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Creation successful")
except socket.error as err:
print ("Creation unsuccessful. Error: %s" %(err))
port = 80
try:
ip = socket.gethostbyname('gaia.cs.umass.edu')
except socket.gaierror:
print("Error resolving host")
sys.exit()
s.connect((ip, port))
print("Connection successful")
print("Connected to %s" %(ip))
try:
s.sendall("GET wireshark-labs/HTTP-ethereal-lab-file3.html HTTP/1.1rnHost: gaia.cs.umass.edurnrn".encode())
while True:
data = s.recv(1024)
print("Data received")
print(data.decode('UTF-8'))
if not data:
print("No Data")
s.close()
break
except socket.gaierror:
print("Error sending data")
sys.exit()
And this is the error I receive:
HTTP/1.1 400 Bad Request
Date: Mon, 23 Nov 2020 07:04:03 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.12 mod_perl/2.0.11 Perl/v5.16.3
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>
Trying to get this GET request to work is driving me insane, thanks in advance for any help you all send my way.
3
Answers
Sorry cannot write this as a comment as including the output is necessary. I was trying things with your code and when I changed the
host
and thewireshark-labs
part ..a strange thing happened. I changed it tos.sendall(b"GET / HTTP/1.1rnHost: www.cnn.comrnrn")
to understand how this works…and the response I get is…
Which is what you are looking for. It would seem that the
host
value in thesendall
command is overwritten somewhere. Don’t know how much of a partwireshark-labs
part has in this. Hope the information helps. Also when I paste your code in pycharm it gives a warning heres.connect((ip, port))
s could be undefined
.P.S:- I’m not an expert in
socket
programming. Please excuse if there is some mistake. Just trying to help.This might help somebody, i had the same issue with the 400 Bad Request.
I looked at the answer of Abhishek Rai (Thank you!) above and i realized my http request had only the newline escapes n and lacked the r carriage return escape, when i changed all the n for rn my issue was fixed.
This was my error reading "HTTP/1.1 400 Bad Request" after entering the codes at the bottom.
Where nn was, I replaced it with rnrn; it worked after that.
At the bottom are the adjustment and results.
HTTP/1.1 200 OK
Date: Thu, 08 Dec 2022 06:52:18 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Sat, 13 May 2017 11:22:22 GMT
ETag: "a7-54f6609245537"
Accept-Ranges: bytes
Content-Length: 167
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Connection: close
Content-Type: text/plain
But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already s
ick and pale with grief