'Maintaining Connections on a HTTP proxy in python

    import main
    import socket, sys, os, _thread

    def proxy_setup(port):

    #port number
    host = ''   #proxy will run on localhost

    print("Proxy server running on ", host, ":", port)                                     

    try:
        #socket creation and host and port binding
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind((host, port))
        s.listen(main.BACKLOG)
    
    except socket.error as e:
        (value, message) = e.args
        if s:
            s.close()
        print(message)
        sys.exit(1)

    while 1:
        s.listen(2)
        conn, client_addr = s.accept()
        print(conn)
        #IP = str(input("Please enter your chosen IP: "))
        _thread.start_new_thread(proxy_thread, (conn, client_addr))

    s.close()

def output(type,request,address):

    if "Block" in type or "Blacklist" in type:
        colornum = 91
    elif "Request" in type:
        colornum = 92
    elif "Reset" in type:
        colornum = 93

    print("\033[",colornum,"m",address[0],"\t",type,"\t",request,"\033[0m")

def proxy_thread(conn, client_addr):

    #load_balancer()
    # get the request from browser

    request = conn.recv(main.MAX_DATA_RECV)
    #print(request)
    # parse the first line
    first_line = request.split(b'\n')[0]

    # get url
    url = first_line.split(b' ')[1]


    output("Request",first_line,client_addr)
    # print "URL:",url
    # print

    # find the webserver and port
    http_pos = url.find(b"://")          # find pos of ://
    if (http_pos==-1):
        temp = url
    else:
        temp = url[(http_pos+3):]       # get the rest of url

    port_pos = temp.find(b":")           # find the port pos (if any)

    # find end of web server
    webserver_pos = temp.find(b"/")
    if webserver_pos == -1:
        webserver_pos = len(temp)

    webserver = ""
    port = -1
    if (port_pos==-1 or webserver_pos < port_pos):      # default port
        port = 80
        webserver = temp[:webserver_pos]
    else:       # specific port
        port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
        webserver = temp[:port_pos]
    try:
        # create a socket to connect to the web server
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(("192.168.1.198", 0))
        s.connect((webserver, port))
        s.send(request)         # send request to webserver

        while 1:
            # receive data from web server
            data = s.recv(main.MAX_DATA_RECV)
        if (len(data) > 0):
            # send to browser
            conn.send(data)
        else:
            break
    #s.close()
    #conn.close()

except socket.error as e:
    (value, message) = e.args
    if s:
        s.close()
    if conn:
        conn.close()
    output("Peer Reset",first_line,client_addr)
    sys.exit(1)

I am trying to maintain a http connection when it goes through my web proxy. Currently, every time something from the same url is requested, it uses a new connection. Any idea how I can reuse the same connection if the request comes from a url that's already been requested. I have looked at HTTPConnectionPool but I am not sure how I would implement that here. I use this proxy by redirecting my browser to the localhost and whatever port the proxy is on.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source