title: How to communicate with a HTTP/HTTPS server via Python sockets?
body: <p>I'm trying to create a TCP socket server in Python that after receiving a string of bytes from a client passes the received data(without knowing what it's actually inside, assuming it's a valid HTTP request) to a HTTP or HTTPS proxy and waits for results, my code looks like this:</p>
  
  <pre><code>import socket
  
  def test(host, port):
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect((socket.gethostbyname(host), int(port))
  
     msg = """GET / HTTP/1.1 
     Host: www.bing.com    
     User-Agent: Firefox
  
     """
  
    sent_count = sock.send(msg)
    recv_value = sock.recv(2048)
    print('recvieved:',)
    print str(recv_value)
    pass
  
  if __name__ == '__main__':
     test(host='x.x.x.xx', port='80') # a https proxy server
     test(host='yy.yy.yy.yy', port='80') # a http proxy server
  </code></pre>
  
  <p>But when i connect to the HTTP proxy server it returns something like:</p>
  
  <pre><code>HTTP/1.1 404 Not Found
  </code></pre>
  
  <p>And when i connect to the HTTPS proxy server it shows something like:</p>
  
  <pre><code>HTTP/1.0 400 Bad Request
  </code></pre>
  
  <p>So wanted to ask if anybody know how could i send HTTP requests to HTTP/HTTPS servers via sockets in Python? or how can i redirect arbitrary strings of data toward HTTP/HTTPS proxy servers in general in Python using sockets?, any suggestions are very much appreciated, thanks in advance.</p>