http代理服务器仅适用于https站点 [英] http proxy server only working for https sites
问题描述
我正在尝试使用此代码来创建HTTP代理缓存服务器。当我运行代码时,它开始运行并连接到端口和所有东西,但是当我尝试从浏览器连接时,例如,如果我输入localhost,它会在55555上打开一个端口:52523 / www.google.com它工作正常但是当我尝试其他网站特别是HTTP时,例如localhost:52523 / www.microcenter.com或者只是localhost:52523 / google.com它会显示localhost没有发送任何数据。
ERR_EMPTY_RESPONSE并在控制台中显示异常,虽然它在我的计算机上创建了缓存文件。
I am trying to use this code to create an HTTP proxy cache server. When I run the code it starts running and connects to the port and everything but when I try to connect from the browser, for example, it opens a port on 55555 if I type in localhost:52523/www.google.com it works fine but when I try other sites specifically HTTP, for example, localhost:52523/www.microcenter.com or just localhost:52523/google.com it will display localhost didn’t send any data. ERR_EMPTY_RESPONSE and shows an exception in the console though it creates the cache file on my computer.
我想知道如何编辑代码以便就像我通常在浏览器上那样访问任何网站而不使用代理服务器。它应该可以使用www.microcenter.com
I would like to find out how to edit the code so that I can access any website just as I would normally do on the browser without using the proxy server. It should be able to work with www.microcenter.com
import socket
import sys
import urllib
from urlparse import urlparse
Serv_Sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # socket.socket
function creates a socket.
port = Serv_Sock.getsockname()[1]
# Server socket created, bound and starting to listen
Serv_Sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # socket.socket
function creates a socket.
Serv_Sock.bind(('',port))
Serv_Sock.listen(5)
port = Serv_Sock.getsockname()[1]
# Prepare a server socket
print ("starting server on port %s...,"%(port))
def caching_object(splitMessage, Cli_Sock):
#this method is responsible for caching
Req_Type = splitMessage[0]
Req_path = splitMessage[1]
Req_path = Req_path[1:]
print "Request is ", Req_Type, " to URL : ", Req_path
#Searching available cache if file exists
url = urlparse(Req_path)
file_to_use = "/" + Req_path
print file_to_use
try:
file = open(file_to_use[5:], "r")
data = file.readlines()
print "File Present in Cache\n"
#Proxy Server Will Send A Response Message
#Cli_Sock.send("HTTP/1.0 200 OK\r\n")
#Cli_Sock.send("Content-Type:text/html")
#Cli_Sock.send("\r\n")
#Proxy Server Will Send Data
for i in range(0, len(data)):
print (data[i])
Cli_Sock.send(data[i])
print "Reading file from cache\n"
except IOError:
print "File Doesn't Exists In Cache\n fetching file from server \n
creating cache"
serv_proxy = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host_name = Req_path
print "HOST NAME:", host_name
try:
serv_proxy.connect((url.host_name, 80))
print 'Socket connected to port 80 of the host'
fileobj = serv_proxy.makefile('r', 0)
fileobj.write("GET " + "http://" + Req_path + " HTTP/1.0\n\n")
# Read the response into buffer
buffer = fileobj.readlines()
# Create a new file in the cache for the requested file.
# Also send the response in the buffer to client socket
# and the corresponding file in the cache
tmpFile = open(file_to_use, "wb")
for data in buffer:
tmpFile.write(data)
tcpCliSock.send(data)
except:
print 'Illegal Request'
Cli_Sock.close()
while True:
# Start receiving data from the client
print 'Initiating server... \n Accepting connection\n'
Cli_Sock, addr = Serv_Sock.accept() # Accept a connection from client
#print addr
print ' connection received from: ', addr
message = Cli_Sock.recv(1024) #Recieves data from Socket
splitMessage = message.split()
if len(splitMessage) <= 1:
continue
caching_object(splitMessage, Cli_Sock)
推荐答案
您的错误与URI方案(http或https)无关,但与文件和套接字使用无关。
Your errors are not related to URI scheme (http or https) but to files and socket use.
当您尝试打开时文件包含:
When you are trying to open a file with:
file = open(file_to_use[1:], "r")
您正在传递非法文件路径( http ://ebay.com/ 在您的示例中。)
you are passing an illegal file path (http://ebay.com/ in your example).
当您使用URI时,您可以使用像 urlparse ,这样你就可以更好地处理架构,主机名等......
As you are working with URIs, you could use a parser like urlparse, so you can handle better the schema, hostname, etc...
例如:
url = urlparse(Req_path)
file_to_use = url.hostname
file = open(file_to_use, "r")
并使用只有主机名作为文件名。
and use only the hostname as a file name.
另一个问题是使用套接字。函数 connect
应该接收主机名,而不是主机名,这是你正在做的架构。再次,在解析器的帮助下:
Another problem is with the use of sockets. Function connect
should receive hostname, not hostname with schema which is what you are doing. Again, with the help of the parser:
serv_proxy.connect((url.hostname, 80))
除此之外,您不会在客户端上调用 listen
(请参阅示例),以便您删除该行。
Besides that, you do not call listen
on a client (see examples), so you can remove that line.
最后,再次创建新文件,使用主机名:
Finally, again to create the new file, use the hostname:
tmpFile = open(file_to_use, "wb")
这篇关于http代理服务器仅适用于https站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!