Python:我无法连接端口80以获取我想要的网页

时间:2016-11-26 13:28:13

标签: python sockets proxy server

我输入了http://127.0.0.1:443/www.google.com,我收到了非法请求'在命令提示符下。 在该代码中连接到端口80并不起作用。 有什么问题,如何使用代理服务器获取此网站? (我已经设置了internet browser-127.0.0.1(ip),443(port)

的代理设置

`Hello.py

from socket import *
import sys
from idlelib.rpc import response_queue

if len(sys.argv) <= 1:
print ('Usage : "python ProxyServer.py server_ip"\n [server_ip : It is the IP Address Of Proxy Server')
sys.exit(2)

# Create a server socket, bind it to a port and start listening
tcpSerSock = socket(AF_INET, SOCK_STREAM)

port = 443
max_connections = 2
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind((sys.argv[1],port))
tcpSerSock.listen(max_connections)

while 1:
# Start receiving data from the client
print ('Ready to serve...')

tcpCliSock, addr = tcpSerSock.accept()
print ('Received a connection from:', addr)

message = tcpCliSock.recv(1024)
print ('Msg: ' ,message)

# Extract the filename from the given message
print ('Msg decoded: ', message.decode().split()[1])
filename = message.decode().split()[1].partition("/")[2]
print ('File name: ', filename)

fileExist = "false"
filetouse = "/" + filename

print ('File touse: ', filetouse)
try:
    # Check whether the file exist in the cache
    f = open(filetouse[1:], "r")                      
    outputdata = f.readlines()                        
    fileExist = "true"
    print ('File exists')
    # ProxyServer finds a cache hit and generates a response message
    tcpCliSock.send("HTTP/1.0 200 OK\r\n")            
    tcpCliSock.send("Content-Type:text/html\r\n")
    resp = ""
    for s in outputdata:
        resp += s

    tcpCliSock.send(resp)
    print ('Read from cache')  

# Error handling for file not found in cache
except IOError:
    if fileExist == "false": 
        # Create a socket on the proxy server
        c = socket(AF_INET, SOCK_STREAM)
        hostn = filename.replace("www.","",1)
        print ('File doesn\'t exist')
        print (hostn)
        try:
            # Connect to the socket to port 80
            c.connect((hostn, 80))

            # Create a temporary file on this socket and ask port 80
            # for the file requested by the client
            fileobj = c.makefile('r', 0)               
            fileobj.write("GET " + "http://" + filename + " HTTP/1.0\n\n")  

            # Read the response into buffer
            resp = c.recv(4096)
            response = ""
            while resp:
                response += resp
                resp = c.recv(4096)

            # 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("./" + filename,"w")  
            tmpFile.write(response)
            tmpFile.close()
            tcpCliSock.close()

        except:
            print ("Illegal request")                                            
    else:
        # HTTP response message for file not found
        pass
# Close the client and the server sockets    
tcpCliSock.close()`

0 个答案:

没有答案