views:

40

answers:

0

This proxy server connects to another proxy server which is connected to the internet . The problem with this is that authentication etc works fine but the Server receives only the header from the remote server and not the HTML.How to fix this? please help


USER='someUsername'
PASS='somePassword'

import asyncore
import asynchat
import socket
import base64




if __name__ == "__main__":




    class Receiver(asynchat.async_chat):
        def __init__(self,sock):
            asynchat.async_chat.__init__(self,sock)
            self.set_terminator("\r\n")
            self.data = []
            self.buf2=''''''
            self.sender=Sender(self)
            self.token= base64.encodestring('%s:%s' % (USER,PASS)).strip()
        def collect_incoming_data(self,data):
            self.data.append(data)
        def found_terminator(self):
            buf=''.join(self.data)
            self.handle(buf)
            self.data=[]

        def handle_close(self):

            self.sender.close_when_done()
            self.close()
        def handle(self,buf):



            if buf[:19] == 'Proxy-Connection: k':
                self.buf2+=buf+'\r\n'+'Proxy-Authorization: Basic %s' % self.token+'\r\n'

            elif buf in ['\r\n','\n','']:
                self.sender.push(self.buf2+'\r\n'*2)
                #print(self.buf2+'\r\n'*2)
                self.buf2=''''''

            else:
                self.buf2+=buf+'\r\n'



    class Sender(asynchat.async_chat):
        def __init__(self,receiver):
            asynchat.async_chat.__init__(self)
            self.receiver=receiver
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.connect(('10.1.1.19',80))
            self.set_terminator("\r\n")
            self.data = []



        def collect_incoming_data(self,data):

            self.data.append(data)
        def found_terminator(self):
            buf=''.join(self.data)

            self.receiver.push(buf+'\r\n')
            print (buf)
            self.data=[]
        def handle_close(self):

            self.receiver.close_when_done()
            self.close()

    class ProxyServer(asyncore.dispatcher):
        def __init__(self,port):
            asyncore.dispatcher.__init__(self)
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.bind(('',port))
            self.listen(5)
            self.set_reuse_addr()
        def handle_accept(self):
            asyncore.dispatcher.handle_accept(self)
            s,addr=self.accept()
            r=Receiver(s)
    c=ProxyServer(9092)
    try:
        asyncore.loop()
    except KeyboardInterrupt:pass