Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Python套接字与HTTP/HTTPS服务器通信?_Python_Sockets_Http_Https_Proxy - Fatal编程技术网

如何通过Python套接字与HTTP/HTTPS服务器通信?

如何通过Python套接字与HTTP/HTTPS服务器通信?,python,sockets,http,https,proxy,Python,Sockets,Http,Https,Proxy,我正在尝试用Python创建一个TCP套接字服务器,在从客户端接收到一个字节字符串后,它将接收到的数据(假设它是一个有效的HTTP请求,则不知道它实际上在其中)传递到HTTP或HTTPS代理并等待结果,我的代码如下所示: import socket def test(host, port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((socket.gethostbyname(host

我正在尝试用Python创建一个TCP套接字服务器,在从客户端接收到一个字节字符串后,它将接收到的数据(假设它是一个有效的HTTP请求,则不知道它实际上在其中)传递到HTTP或HTTPS代理并等待结果,我的代码如下所示:

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
但是,当我连接到HTTP代理服务器时,它会返回如下内容:

HTTP/1.1 404 Not Found
HTTP/1.0 400 Bad Request
当我连接到HTTPS代理服务器时,它会显示如下内容:

HTTP/1.1 404 Not Found
HTTP/1.0 400 Bad Request

所以我想问一下,是否有人知道我如何通过Python中的套接字向HTTP/HTTPS服务器发送HTTP请求?或者如何使用套接字将任意字符串的数据重定向到Python中的HTTP/HTTPS代理服务器?非常感谢您的建议,提前感谢。

您尝试通过原始套接字与HTTP服务器通信有什么好的理由吗?我正在考虑将发送到localhost端口的HTTP请求重定向到HTTP或HTTPS代理服务器,而无需打开或解析包含实际请求的字符串,如果可能的话。看起来您正在尝试创建TCP代理,那么检查现有的解决方案可能是一个好主意,比如感谢维德,但我只想将机器端口上的任何内容重定向到Python中的代理服务器列表(取决于某些情况)。我无意创建自己的(成熟的)代理服务器。这正是上面链接中的代理所做的-它在一个地址\端口上接收原始TCP流量,并将其重定向到另一个地址\端口。