1

I am trying to make request but google.com returns status 400, but It should be 302. What's wrong with my request? Do i need additional request header? Any ideas?

Current code:

import socket
host = "www.google.com"
port = 80

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host,port))
client.send("GET / HTTP1.1\r\nHost: www.google.com\r\n\r\n")

response = client.recv(4096)
print response

Response:

HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Content-Length: 1504
Date: Mon, 07 Sep 2015 16:25:02 GMT
Server: GFE/2.0

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/logos/errorpage/error_logo-150x54.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/logos/errorpage/error_logo-150x54-2x.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/logos/errorpage/error_logo-150x54-2x.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/logos/errorpage/error_logo-150x54-2x.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//w
ww.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>400.</b> <ins>That’s an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>
2
  • 1
    If you want to use http, do yourself a favour and use requests. That will save you a lot of headaches. Commented Sep 7, 2015 at 20:36
  • @RolandSmith Thanks, you'e right, I use requests and also pycurl in my backend applications but i want to learn socket programming, do penetration tests and make some TCP/UDP based tasks. HTTP requests library is not enough for me at the moment :) Commented Sep 7, 2015 at 20:50

1 Answer 1

2

In the string you use in the send function, you have missed a slash, when specifying the HTTP protocol version.

client.send("GET / HTTP1.1\r\nHost: www.google.com\r\n\r\n")

should be:

client.send('GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n')
Sign up to request clarification or add additional context in comments.

2 Comments

Solved, thanks man. I also noticed that to solve these kind of problems I can use wireshark and compare content with packets generated by higher level libraries or software, like Advanced Rest Client etc.
No problem. I wanted to test it with ngrep on linux, but fortunately I saw the typo. It's a good plan to check using packet traces because you can see a lot more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.