繁体   English   中英

HTTP 代理服务器 [Python]

[英]HTTP Proxy server [Python]

我在这里有一个用于工作的 HTTP 代理服务器的代码。 如何创建另一个名为“客户端”的程序? 客户端将通过代理服务器向多个 Web 服务器发送 HTTP GET 请求。 客户端程序连接到代理并向以下 3 个网站发送 HTTP GET 请求:(www.google.com、www.yahoo.com、www.stackoverflow.com),间隔为 30 秒。

- 我的总体问题是如何从 python 向代理服务器发送 HTTP GET 请求,而不是我的网络浏览器?

OSX 10.10.3 Python 3.4

当我在终端中调用此代理时:

python 1869.py 2000

您可以提供任何端口号来代替 2000。

输出:

starting server ....
Initiating server... 
 Accepting connection 

然后在我的浏览器中(我使用的是最新版本的 chrome)我输入:

localhost:2000/www.stackoverflow.com 

我的终端输出是:

request is  GET  to URL :  www.stackoverflow.com
/www.stackoverflow.com
File Present in Cache

HTTP/1.1 301 Moved Permanently

Content-Type: text/html; charset=UTF-8

Location: http://stackoverflow.com/

Date: Thu, 07 May 2015 17:45:40 GMT

Content-Length: 148

Connection: close

Age: 0



<head><title>Document Moved</title></head>

<body><h1>Object Moved</h1>This document may be found <a HREF="http://stackoverflow.com/">here</a></body>
Reading file from cache

Initiating server... 
 Accepting connection

代理代码:

import socket
import sys
if len(sys.argv) <= 1: 
    print 'Usage: "python S.py port"\n[port : It is the port of the Proxy Server'
    sys.exit(2)

# Server socket created, bound and starting to listen
Serv_Port = int(sys.argv[1]) # sys.argv[1] is the port number entered by the user
Serv_Sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # socket.socket function creates a socket.

# Prepare a server socket
print "starting server ...."
Serv_Sock.bind(('', Serv_Port))
Serv_Sock.listen(5)


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
    file_to_use = "/" + Req_path
    print file_to_use
    try:
        file = open(file_to_use[1:], "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((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("./" + Req_path, "wb")
            for i in range(0, len(buffer)):
                tmpFile.write(buffer[i])
                Cli_Sock.send(buffer[i])
        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)

您可以从 linux 终端使用 httpie:

http [get/post] http://host:port/link_name/

在您的应用程序中,您可以使用请求:pip install requests

import requests

response = requests.get(url='url', proxies='proxies')
response.close()
print(response)

暂无
暂无

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

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