colinzcli
colinzcli

Reputation: 81

python socket bad request 400

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?

Upvotes: 5

Views: 12986

Answers (3)

Michael Seltene
Michael Seltene

Reputation: 591

I tried with HTTP 1.0 and it worked:

m = 'GET / HTTP/1.0\r\nHost: www.cnn.com\r\n\r\n'

Upvotes: 5

Adam Rosenfield
Adam Rosenfield

Reputation: 400274

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.

Upvotes: 8

Peter Pei Guo
Peter Pei Guo

Reputation: 7870

Add Host header:

m = 'GET / HTTP/1.1\r\nHost: www.cnn.com\r\n\r\n'

replace www.cnn.com with your ip address

Upvotes: 2

Related Questions