简体   繁体   English

Web服务器的Python套接字编程

[英]Python socket programming for Webserver

I have created a proxy server that receives requests, searches for the requested file in its cache. 我已经创建了一个接收请求的代理服务器,在其缓存中搜索请求的文件。 If available it returns the cached file. 如果可用,它将返回缓存的文件。 If file is not available then it will ask the actual server, gets it, stores it in the cache and returns the file to the client. 如果文件不可用,它将询问实际的服务器,将其获取,将其存储在缓存中,然后将文件返回给客户端。

Following is the code: 以下是代码:

from socket import *
import sys

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)
tcpSerSock.bind((sys.argv[1], 8888))
tcpSerSock.listen(100)

while 1:
    # Strat receiving data from the client
    print 'Ready to serve...'
    tcpCliSock, addr = tcpSerSock.accept()
    print 'Received a connection from:', addr
    message = tcpCliSock.recv(1024)
    print message
    # Extract the filename from the given message
    print message.split()[1]
    filename = message.split()[1].partition("/")[2]
    print filename
    fileExist = "false"
    filetouse = "/" + filename
    print filetouse
    try:
        # Check wether the file exist in the cache
        f = open(filetouse[1:], "r")                      
        outputdata = f.readlines()                        
        fileExist = "true"
        # 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")
        for i in range(0, len(outputdata)):               
            tcpCliSock.send(outputdata[i])
            print 'Read from cache'     
    # Error handling for file not found in cache
    except IOError:
        if fileExist == "false": 
            # Create a socket on the proxyserver
            c = socket(AF_INET, SOCK_STREAM)            
            hostn = filename.replace("www.","",1)         
            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
                buff = 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("./" + filename,"wb")  
                for line in buff:                                                     
                    tmpFile.write(line);                                               
                    tcpCliSock.send(line);
            except:
                print "Illegal request"                                               
        else:
            # HTTP response message for file not found
            tcpCliSock.send("HTTP/1.0 404 sendErrorErrorError\r\n")                             
            tcpCliSock.send("Content-Type:text/html\r\n")
            tcpCliSock.send("\r\n")
    # Close the client and the server sockets    
    tcpCliSock.close() 
  tcpSerSock.close()

But for every file I request I only get an "illegal request" message printed. 但是对于我请求的每个文件,我只会收到“非法请求”消息。 There seems to be an issue that the proxy server actually is not able to retrieve the requested file by the client. 似乎存在一个问题,即代理服务器实际上无法由客户端检索请求的文件。 Can someone tell me where I can improve the code. 有人可以告诉我在哪里可以改进代码。 This is the first time I am coding in Python so please mention any minor errors. 这是我第一次使用Python进行编码,因此请提及任何较小的错误。

Your request is illegal. 您的请求是非法的。 For normal http servers, GET must not contain a URL, but only the path. 对于普通的http服务器,GET不得包含URL,而只能包含路径。 The rest of your proxy contains also many errors. 代理的其余部分还包含许多错误。 You probably want to use sendall everywhere you use send. 您可能想在所有使用send的地方都使用sendall。 recv can receive less that one message, so you have to handle this case also. recv收到的信息少于一条,因此您也必须处理这种情况。 Why do you use the strings "true" and "false" instead of True and False? 为什么使用字符串“ true”和“ false”代替True和False? There is a security hole, as you can read any file on your computer through your proxy. 有一个安全漏洞,因为您可以通过代理读取计算机上的任何文件。 Reading binary files won't work. 读取二进制文件不起作用。 You don't close opened files. 您不会关闭打开的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM