title: python socket how to properly redirect http/s requests using the same socket connection?
body: <p>I've got here a code that sends an HTTPS request.
  My problem is handling redirection requests using the same socket connection.</p>
  
  <p>I know that the requests module can handle this redirects very well but this code
  is for a proxy server that I'm developing. </p>
  
  <p>Any help would be appreciated. Thanks!</p>
  
  <pre><code>import socket, ssl
  from ssl import SSLContext
  
  HOST = "www.facebook.com"
  PORT = 443
  
  ContextoSSL = SSLContext(protocol=ssl.PROTOCOL_SSLv23)
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sslSocket = ContextoSSL.wrap_socket(sock, server_hostname=HOST)
  sslSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  sslSocket.connect((HOST, PORT))
  sslSocket.do_handshake()
  
  der = sslSocket.getpeercert(binary_form=True)
  pem_data = ssl.DER_cert_to_PEM_cert(der)
  #print(pem_data) # print certificate
  
  
  
  '''1st request'''
  headers = \
      "GET / HTTP/1.1\r\n"\
      "Host: www.facebook.com\r\n"\
      "User-Agent: python-requests/2.22.0\r\n"\
      "Accept-Encoding: gzip, deflate\r\nAccept: */*\r\n"\
      "Connection: keep-alive\r\n\r\n"
  print("\n\n" + headers)
  
  sslSocket.send(headers.encode()) # send request
  response = sslSocket.recv(9999)
  print(response) # print receive response
  
  
  
  
  '''2nd request''' # on this redirect with cookie set, response should be 200 OK 
  cookie, location = "", ""
  for line in response.decode().splitlines():
      if "Set-Cookie" in line:
          cookie = line.replace("Set-Cookie: ", "").split(";")[0]
  
      if "Location" in line:
          location = line.replace("Location: ", "").split("/")[3]
  
  print(cookie, location)
  
  headers = \
      f"GET /{location} HTTP/1.1\r\n"\
      "Host: www.facebook.com\r\n"\
      "User-Agent: python-requests/2.22.0\r\n"\
      "Accept-Encoding: gzip, deflate\r\nAccept: */*\r\n"\
      "Connection: keep-alive\r\n"\
      f"Cookie: {cookie}\r\n\r\n"
  print("\n\n" + headers)
  
  sslSocket.send(headers.encode()) # send request
  response = sslSocket.recv(9999)
  print(response) # print received response
  </code></pre>