开发者

Http through telnet with python and twisted

开发者 https://www.devze.com 2023-04-03 10:35 出处:网络
This is what I want to do: web-browser --> connect to remote server through telnet(server1) --> to squid-proxy(which requires authentication) through telnet on port 80(server2)

This is what I want to do:

web-browser --> connect to remote server through telnet(server1) --> to squid-proxy(which requires authentication) through telnet on port 80(server2)

I have written a small python script that uses Twisted (here :

#! /usr/bin/python
from twisted.internet import reactor, protocol
from twisted.web import http
from telnetlib import Telnet
import getpass
from sys import stdout

class datareceiver(protocol.Protocol):
    def dataReceived(self,data):
        self.telnet_con.write(data)
        stdout.write( self.telnet_con.read_all() )

    def connectionMade(data):
        stdout.write("\nA connection was made to this server\n")

def main():
    server1 = "10.1.1.1"
    #user = raw_input("Enter your remote account: ")
    password = getpass.getpass()
    tn = Telnet(server1)

    if password:
        tn.read_until("Password: ")
        tn.write(password + "\n")

    #This is server2
    tn.write("telnet 10.1.1.10 开发者_高级运维80 \n")


    #serverfac = protocol.Factory()
    serverfac = http.HTTPFactory()
    datareceiver.telnet_con = tn
    serverfac.protocol = datareceiver
    reactor.listenTCP(9229,serverfac)

    reactor.run()
    tn.write("exit\n")

    print tn.read_all()

if __name__ == "__main__":
    main()

But then I realized I'm doing it the wrong way, my shell is getting all the replies from squid instead of the browser. Can someone just outline a correct way of doing this? Should I use something else instead of twisted?


I think you want to look at twisted.web.server.Site and twisted.web.resource.Resource. Also, since you are using Twisted, you'll probably want to use twisted.protocols.telnet.Telnet for the telnet connection, otherwise your application will not be asynchronous.

This blog post and this answer here may also be useful.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号