skip to Main Content

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


  1. 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 the wireshark-labs part ..a strange thing happened. I changed it to
    s.sendall(b"GET / HTTP/1.1rnHost: www.cnn.comrnrn") to understand how this works…
    and the response I get is…

    Creation successful
    Connection successful
    Connected to 128.119.245.12
    Data received
    HTTP/1.1 200 OK
    Date: Mon, 23 Nov 2020 07:48:51 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
    Last-Modified: Tue, 01 Mar 2016 18:57:50 GMT
    ETag: "a5b-52d015789ee9e"
    Accept-Ranges: bytes
    Content-Length: 2651
    Content-Type: text/html; charset=UTF-8
    
    <html>
    <head>
    <title>Computer Network Research Group - UMass Amherst
    </title>
    </head>
    <body bgcolor="#ffffff">
    <center>
    <p><img 
    src="cnrg_imap.jpg" 
    border="0" usemap="#cnrg_imapMAP">
    <map name="cnrg_imapMAP">
    <area coords="290,177,407,205" shape="rect" href="/networks/resources/index.html">
    <area coords="163,178,275,205" shape="rect" href="/networks/education/index.html">
    <area coords="62,165,145,191" shape="rect" href="/search.html">
    <area coords="6,63,157,90" shape="rect" href="/networks/collaborations.html">
    <area coords="64,7,146,34" shape="rect" href="/networks/people.html">
    <area coords="163,7,270,33" shape="rect" href="/networks/research.html">
    <area coords="288,6,417,33" shape="rect" 
    href="/networks/
    Data received
    publications.html">
    </map>
    <P>
    <BR>
    <BR>
    <P>
    <table width=100% border=0 cellpadding=0 cellspacing=0>
      <tr>
        <td width=60>&nbsp;</td>
        <td width="10%">&nbsp;</td>
        <td width="70%"> 
    <font face="Helvetica,Arial,TimesNewRoman" color="#000000">
    <h4>
    The <a href="/index.html"> Computer Networks Research Group </a> in the <a href = "https://www.cics.umass.edu/"> School of Information and Computer Sciences</a> at
    the <a href = "http://www.umass.edu/"> University of Massachusetts, Amherst </a> is led by Professors <a href = "http://www-net.cs.umass.edu/kurose/index.html">Jim Kurose </a>, and <a href = "/personnel/towsley.html">Don Towsley</a>. 
    Our research spans a broad range of topics in networking, including network protocols and architecture, modeling and analysis, sensor networks, wireless networks, and network measurement. We seek a principled understanding of new and emerging areas through a complementary mix of theoretical and applied experimental research. 
      </td>
      <td width="10%">&nbsp;</td>
     </
    Data received
    tr>
    </table>
    
     
    </h4>
    
    </font>
    <p>
    <!-- QUP Button & QualNet link markup start -->
    <a href="http://www.scalable-networks.com/customers/qup/index.php">
    <img src="http://www.scalable-networks.com/images/qupmember.gif" border=0
    alt="QualNet  Network Simulator University Program"></a>
    <a href="http://www.scalable-networks.com">QualNet Network Simulator</a>
    <!-- End of QUP Button HTML & QualNet link markup -->
    <p>
    <p>
    <center>
    <font size="1">
    <a href="/networks/people.html"> PEOPLE |</a> 
    <a href="/networks/research.html"> RESEARCH |</a> 
    <a href="http://www-net.cs.umass.edu/networks/publications.html"> PUBLICATIONS |</a> 
    <a href="/networks/collaborations.html"> COLLABORATIONS |</a> 
    <a href="http://gaia.cs.umass.edu/search.html"> SEARCH |</a> 
    <a href="/networks/education/index.html"> EDUCATION |</a> 
    <a href="/networks/resources/index.html"> 
    RESOURCES</a> 
    <p>
    
    </font>
    </center>
    </body>
    </html>
    
    Data received
    
    No Data
    
    Process finished with exit code 0
    

    Which is what you are looking for. It would seem that the host value in the sendall command is overwritten somewhere. Don’t know how much of a part wireshark-labs part has in this. Hope the information helps. Also when I paste your code in pycharm it gives a warning here s.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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. This was my error reading "HTTP/1.1 400 Bad Request" after entering the codes at the bottom.

    import socket
    my_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    my_sock.connect(('data.pr4e.org', 80))
    cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0nn'.encode()
    my_sock.send(cmd)
    
    while True:
        data = my_sock.recv(512)
        if len(data) < 1:
            break
        print(data.decode())
    my_sock.close()
    

    Where nn was, I replaced it with rnrn; it worked after that.
    At the bottom are the adjustment and results.

    import socket
    my_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    my_sock.connect(('data.pr4e.org', 80))
    cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0rnrn'.encode()
    my_sock.send(cmd)
    
    while True:
        data = my_sock.recv(512)
        if len(data) < 1:
            break
        print(data.decode())
    my_sock.close()
    

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search