使用 python 和twisted 通过 telnet 进行 Http

发布于 2024-12-03 19:26:15 字数 1259 浏览 0 评论 0原文

这就是我想做的:

网络浏览器 -->通过telnet连接远程服务器(server1) -->通过端口80(server2)上的telnet到squid-proxy(需要身份验证)

我编写了一个使用Twisted的小python脚本(此处:

#! /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()

但后来我意识到我做错了,我的shell正在获取所有回复来自鱿鱼而不是浏览器。 有人可以概述这样做的正确方法吗? 我应该使用其他东西而不是扭曲吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-12-10 19:26:15

我想你想看看 twisted.web.server .Sitetwisted.web.resource.Resource。另外,由于您使用的是 Twisted,因此您可能需要使用 twisted.protocols.telnet.Telnet 用于 telnet 连接,否则您的应用程序将不是异步的。

这篇博文这里的答案也可能有用。

希望这有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文