Problems when using Pything to connect to website with socket and http

Viewed 25

I'm new to socket programming, and I basically wrote some code using the socket library that was supposed to print metadata and the website content of my desired website - in my case Facebook, http port 80..

My code looks like this:

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('facebook.com',80))
GET = 'GET http://www.facebook.com HTTP/1.0\r\n\r\n'.encode()
s.send(GET)

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

The problem is,when I run the program, I get 'HTTP/1.1 502 Error with server connection' The other outputted metadata at the top looks something like this:

Proxy-Status: server_connection_error; e_isproxyerr= (and a bunch of incomprehensible letters and numbers I won't include here)
Connection: close
Content-Length: 2959

My main goal was to print metadata with no errors and the desired content of the website, but it doesn't seem to be working. It also gave me an html script, but it didn't give me what I was looking for, the printed text on the facebook.com page which says 'Connect with friends and the world around you on Facebook.'

What am I doing/approaching wrong?

1 Answers
GET = 'GET http://www.facebook.com HTTP/1.0\r\n\r\n'.encode()

This is not a valid HTTP request in the first place. The error you get is from your (transparent) proxy in the network but even without such proxy Facebook itself does reject the request with status code 400 Bad Request.

For one, the location given in the request should not contain the full URL but the absolute path only, i.e. / in this case. Additionally there should be a Host header, i.e.

GET = 'GET / HTTP/1.0\r\nHost: facebook.com\r\n\r\n'.encode()

With this the request succeeds and returns a 301 redirect to https://facebook.com/. You would need to extract this and follow the redirect, which also means that you would need to implement support for TLS in your code.

In general: use established HTTP libraries like requests. These are a) simpler to use and b) more likely to work. If you insist on writing your own (like for learning) then please study the relevant standards instead of guessing how the protocol might work.

Related