How to make correct TCP request using python
Question: 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>
    


Answer: 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')
    

