Python proxy - Need help to send HTTP header to the browser
Question: I'm making a proxy for my project and I'm trying to send to the browser
(Firefox) an HTTP header to continue the "Conversation" between me(Proxy
server) and the browser. The issue is: when I'm refreshing any page, the page
Keeping loading. I use socket and select for the proxy:

    
    
    import socket, select
    
    #Sending a message for the waiting list
    def send_Waiting_Messages(wlist):
        for msg in messages_to_send:
            clientSocket, data = msg
            if clientSocket in wlist:
                clientSocket.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n')
                messages_to_send.remove(msg)
    
    serverSocket = socket.socket()
    serverSocket.bind(('0.0.0.0',8080))
    serverSocket.listen(10)
    open_client_sockets = []
    messages_to_send = []
    
    while True:
        rlist, wlist, xlist = select.select([serverSocket] + open_client_sockets, open_client_sockets, [])
        for currentSocket in rlist:
            if currentSocket is serverSocket:
                newSocket, addr = serverSocket.accept()
                open_client_sockets.append(newSocket)
            else:
                data = currentSocket.recv(1024)
                if data == "":
                    open_client_sockets.remove(currentSocket)
                    print 'Conn is closed'
                else:
                    print data
                    messages_to_send.append((currentSocket, 'Hello, ' + data))
        send_Waiting_Messages(wlist)
    

These lines:

    
    
    clientSocket.send('HTTP/1.1 200 OK\r\n')
    clientSocket.send('Content-Type: text/html\r\n\r\n')
    

are for sending the header.

Thanks for helpers!!


Answer: notice that every the `send_Waiting_Messages` function is sending the same
response to all of your connected clients. I reccomend to replace it with the
basic send function when the socket is in the `wlist`.

* * *

**This will probably work**

    
    
    import socket, select
    serverSocket = socket.socket()
    serverSocket.bind(('0.0.0.0',8080))
    serverSocket.listen(10)
    open_client_sockets = []
    
    while True:
        rlist, wlist, xlist = select.select([serverSocket] + open_client_sockets, open_client_sockets, [])
        for currentSocket in rlist:
            if currentSocket is serverSocket:
                newSocket, addr = serverSocket.accept()
                open_client_sockets.append(newSocket)
            else:
                data = currentSocket.recv(2048)
                if data == "":
                    open_client_sockets.remove(currentSocket)
                    print 'Conn is closed'
                else:
                    print data
                    content_to_send = "The content that you want to send"
                    currentSocket.send("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length:"+str(len(content_to_send))+"\r\n\r\n"+content_to_send)
    

