使用 python 和twisted 通过 telnet 进行 Http
这就是我想做的:
网络浏览器 -->通过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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你想看看 twisted.web.server .Site 和 twisted.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!