Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python socket bad request 400

Tags:

python

http

I run my program which is used to request a text file on a website, using python(2.7.6) socket.

import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
h='58.68.237.xxx'
p=80
s.connect((h,p))
m='GET / HTTP/1.1\r\n\r\n'
s.sendall(m)
r=s.recv(4096)
print r

And, I got the output:

HTTP/1.1 400 Bad Request\r\n
Date: Mon, 13 Oct 2014 02:46:15 GMT\r\n
Server: Apache/2.2.3 (CentOS)\r\n
Content-Length: 300\r\nConnection: close\r\n
Content-Type: text/html; charset=iso-8859-1\r\n
\r\n
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0 //EN">\n
<html><head>\n
<title>400 Bad Request</title>\n
</head><body>\n
<h1>Bad Request</h1>\n
<p>Your browser sent a request that this server could not understand.<br/>\n
</p>\n<hr>\n
<address>Apache/2.2.3 (CentOS) Server at 127.0.0.1 Port 80</address>\n
</body></html>\n

Question: What is wrong in my code? How can I fix it for a good request?

like image 634
colinzcli Avatar asked Dec 19 '22 10:12

colinzcli


2 Answers

HTTP 1.1 requires that you transmit a Host header with all requests. From RFC 1626 section 14.23:

A client MUST include a Host header field in all HTTP/1.1 request messages . [...] All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message which lacks a Host header field.

The reason for the Host header is to allow the server to disambiguate which website is being accessed, if multiple websites are being served up on the same IP address.

Alternatively, you can use HTTP 1.0 instead of HTTP 1.1. HTTP 1.0 doesn't require the Host header, so it may work if the server you're connecting to only has a single website on it, but if it's hosting multiple websites, you'll probably still get a 400 error.

like image 50
Adam Rosenfield Avatar answered Dec 27 '22 06:12

Adam Rosenfield


I tried with HTTP 1.0 and it worked:

m = 'GET / HTTP/1.0\r\nHost: www.cnn.com\r\n\r\n'
like image 23
Michael Seltene Avatar answered Dec 27 '22 05:12

Michael Seltene