这是我到目前为止提出的一些代码......
from twisted.internet import protocol,reactor
from twisted.internet.protocol import Protocol,ClientFactory
class serverprotocol(Protocol):
def dataReceived(self,data):
print "[+] got \n" + data
factory = protocol.ClientFactory()
factory.protocol = Clientp
global xx
xx=data
print xx
reactor.connectTCP("127.0.0.1",80,factory)
class Clientp(Protocol):
def connectionMade(self):
print "sending " + xx
self.transport.write(xx)
def dataReceived(self,data):
print "+ got reply" + data
factory = protocol.ServerFactory()
factory.protocol = serverprotocol
reactor.listenTCP(3333, factory)
reactor.run()
我想知道如何将客户端类中收到的数据发送回客户端 连接3333端口。
如果有更好的方法来编码,请告诉我。
答案 0 :(得分:2)
这是一个Twisted常见问题解答:"How do I make input on one connection result in output on another"?
但它并不是一个扭曲的问题。它更像是一个基本的Python编程问题。
鉴于班级Clientp
,您提出的问题是,如何将论证传递给它,以便它知道以后要做什么?
答案很简单"给它一个__init__
,传递一个参数"
from twisted.internet.protocol import Protocol, Factory
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.internet import reactor
class serverprotocol(Protocol):
def dataReceived(self,data):
print "[+] got \n" + data
def clientProtocol():
return Clientp(data)
endpoint = TCP4ClientEndpoint(reactor, "127.0.0.1", 80)
endpoint.connect(Factory.forProtocol(clientProtocol))
class Clientp(Protocol):
def __init__(self, dataToSend):
self.dataToSend = dataToSend
def connectionMade(self):
self.transport.write(self.dataToSend)
def dataReceived(self,data):
print "+ got reply" + data
reactor.listenTCP(3333,
Factory.forProtocol(serverprotocol))
reactor.run()
如果你想要的是一个完整的端口转发器,你可以简单地使用twistd portforward
或查看twisted.protocols.portforward
中的代码。
答案 1 :(得分:0)
from twisted.internet import protocol,reactor
from twisted.internet.protocol import Protocol,ClientFactory
clients=[]
class serverprotocol(Protocol):
def connectionMade(self):
g.append(self)
def dataReceived(self,data):
factory = protocol.ClientFactory()
factory.protocol = Clientp
global xx
xx=data
reactor.connectTCP("127.0.0.1",80,factory)
class Clientp(Protocol):
def connectionMade(self):
print "sending "
self.transport.write(xx)
def dataReceived(self,data):
print "+ got reply"
for x in clients:
x.transport.write(data)
factory = protocol.ServerFactory()
factory.protocol = serverprotocol
reactor.listenTCP(8080, factory)
reactor.run()
答案 2 :(得分:-1)
因为我有偏见,我将提出一个非扭曲的解决方案:
#!/usr/bin/env python
from uuid import uuid4 as uuid
from circuits import Component
from circuits.net.events import close, connect, write
from circuits.net.sockets import TCPClient, TCPServer
class Client(Component):
channel = "client"
def init(self, sock, host, port, channel=channel):
self.sock = sock
self.host = host
self.port = port
TCPClient(channel=self.channel).register(self)
def ready(self, *args):
self.fire(connect(self.host, self.port))
def disconnect(self, *args):
self.fire(close(self.sock), self.parent.channel)
def read(self, data):
self.fire(write(self.sock, data), self.parent.channel)
class Proxy(Component):
channel = "server"
def init(self, bind, host, port):
self.bind = bind
self.host = host
self.port = port
self.clients = dict()
TCPServer(self.bind).register(self)
def connect(self, sock, host, port):
channel = uuid()
client = Client(
sock, self.host, self.port, channel=channel
).register(self)
self.clients[sock] = client
def disconnect(self, sock):
client = self.clients.get(sock)
if client is not None:
client.unregister()
del self.clients[sock]
def read(self, sock, data):
client = self.clients[sock]
self.fire(write(data), client.channel)
app = Proxy(("0.0.0.0", 3333), "127.0.0.1", 22)
from circuits import Debugger
Debugger().register(app)
app.run()
这可以像使用circuits框架的完整tcp端口转发代理一样工作。
以下是一个示例运行:
终端1(服务器):
Fri May 23 14:05:02
~/circuits
$ ./proxy.py
<registered[server] (<TCPServer/server 48541:MainThread (queued=0) [S]>, <Proxy/server 48541:MainThread (queued=3) [R]> )>
<registered[*] (<Debugger/* 48541:MainThread (queued=0) [S]>, <Proxy/server 48541:MainThread (queued=4) [R]> )>
<started[server] (<Proxy/server 48541:MainThread (queued=3) [R]> )>
<registered[select] (<Select/select 48541:MainThread (queued=0) [S]>, <TCPServer/server 48541:MainThread (queued=0) [S]> )>
<ready[server] (<TCPServer/server 48541:MainThread (queued=0) [S]>, ('0.0.0.0', 3333) )>
<_read[server] (<socket._socketobject object at 0x100ff57c0> )>
<connect[server] (<socket._socketobject object at 0x100ff5980>, '127.0.0.1', 52911 )>
<_read[server] (<socket._socketobject object at 0x100ff5980> )>
<registered[ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6] (<TCPClient/ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6 48541:MainThread (queued=0) [S]>, <Client/ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6 48541:MainThread (queued=0) [S]> )>
<registered[ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6] (<Client/ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6 48541:MainThread (queued=0) [S]>, <Proxy/server 48541:MainThread (queued=3) [R]> )>
<read[server] (<socket._socketobject object at 0x100ff5980>, 'SSH-2.0-OpenSSH_6.2\r\n' )>
<ready[ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6] (<TCPClient/ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6 48541:MainThread (queued=0) [S]> )>
<write[ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6] ('SSH-2.0-OpenSSH_6.2\r\n' )>
<connect[ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6] ('127.0.0.1', 22 )>
<connected[ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6] ('127.0.0.1', 22 )>
<_write[ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6] (<socket._socketobject object at 0x100ff5830> )>
<_read[ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6] (<socket._socketobject object at 0x100ff5830> )>
<read[ee1c155f-d2de-4f09-b05d-5b5e6f4de1d6] ('SSH-2.0-OpenSSH_6.2\r\n' )>
<write[server] (<socket._socketobject object at 0x100ff5980>, 'SSH-2.0-OpenSSH_6.2\r\n' )>
<_write[server] (<socket._socketobject object at 0x100ff5980> )>
<_read[server] (<socket._socketobject object at 0x100ff5980> )>
2号航站楼(客户):
Fri May 23 14:06:26
~
$ ssh -p 3333 localhost
Last login: Fri May 23 14:04:51 2014 from localhost
Fri May 23 14:06:31
~
$ logout
Connection to localhost closed.