How to fix python socket HTTP/1.1 400 BAD REQUEST error

Viewed 143

I am trying this code to get data from pr4e.com. This is a Coursera topic(not an assignment). I tried searching the internet and I got some answers but not for a code similar to this.

import socket

mysock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

mysock.connect(('data.pr4e.org',80))

cmd = 'Get http://data.pr4e.org/romeo.txt HTTP/1.0\n\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if (len(data)<1):
        break
    print(data.decode())
mysock.close()

i have tried changing HTTP/1.0 TO 1.1 and changing '\n\n' to '\n\r\n\r'.

the output i am getting

HTTP/1.1 400 Bad Request                                                                    
Date: Wed, 21 Apr 2021 06:51:18 GMT                                                         
Server: Apache/2.4.18 (Ubuntu)                                                              
Content-Length: 308                                                                         
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>                                                                                        
<hr>                                                                                        
<address>Apache/2.4.18 (Ubuntu) Server at do1.dr-chuck.com Port 80</address>                
</body></html>  
0 Answers
Related