0

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!!

3
  • so, what is the problem? Commented Mar 19, 2015 at 15:05
  • The page is keeping loading Commented Mar 19, 2015 at 15:05
  • I want that the page will response me again and it doesn't work because of this issue Commented Mar 19, 2015 at 15:08

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Try to define content_to_send in the beginning of the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.