之間的TCP中繼我有以下情況:需要幫助建立兩個插座
SomeServer(S) <-> (C)MyApp(S) <-> (C)User
(S) represents a server socket
(C) represents a client socket
從本質上講,MyApp的啓動與SomeServer(SomeServer(S)<通信 - >(C)MyApp的 )並且一旦一些認證例程成功MyApp(S)開始等待(C)用戶連接。 用戶連接,MyApp中繼數據SomeServer到用戶。這發生在兩個的方向。
我有SomeServer(S)< - >(C)MyApp的完美的工作,但我沒能得到MyApp的(S)< - >(C)用戶工作。我得到儘可能用戶連接到MyApp(S),但無法獲得數據中繼!
好的,我希望這是一些清晰的;)現在讓我顯示我的代碼爲MyApp。 btw執行SomeServer和用戶是不是有關解決我的問題,因爲都不可以修改。
我評論了我的代碼,指出我遇到問題。哦,我還應該提到,如果有必要,我沒有問題將整個「服務器部分」廢棄爲其他一些代碼。這是一個POC,所以我的主要焦點是讓功能工作,而不是編寫高效的代碼。謝謝你的時間。
''' MyApp.py module '''
import asyncore, socket
import SSL
# Client Section
# Connects to SomeServer
class MyAppClient(asyncore.dispatcher):
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))
connectionPhase = 1
def handle_read(self):
print "connectionPhase =", self.connectionPhase
# The following IF statements may not make sense
# as I have removed code irrelevant to this question
if self.connectionPhase < 3: # authentication phase
data = self.recv(1024)
print 'Received:', data
# Client/Server authentication is handled here
# Everything from this point on happens over
# an encrypted socket using SSL
# Start the RelayServer listening on localhost 8080
# self.socket is encrypted and is the socket communicating
# with SomeServer
rs = RelayServer(('localhost', 8080), self.socket)
print 'RelayServer started'
# connectionPhase = 3 when this IF loop is done
elif self.connectionPhase == 3: # receiving data for User
data = self.recv(1024)
print 'Received data - forward to User:', data
# Forward this data to User
# Don't understand why data is being read here
# when the RelayServer was instantiated above
# Server Section
# Connects to User
class RelayConnection(asyncore.dispatcher):
def __init__(self, client, sock):
asyncore.dispatcher.__init__(self)
self.client = client
print "connecting to %s..." % str(sock)
def handle_connect(self):
print "connected."
# Allow reading once the connection
# on the other side is open.
self.client.is_readable = True
# For some reason this never runs, i.e. data from SomeServer
# isn't read here, but instead in MyAppClient.handle_read()
# don't know how to make it arrive here instead as it should
# be relayed to User
def handle_read(self):
self.client.send(self.recv(1024))
class RelayClient(asyncore.dispatcher):
def __init__(self, server, client, sock):
asyncore.dispatcher.__init__(self, client)
self.is_readable = False
self.server = server
self.relay = RelayConnection(self, sock)
def handle_read(self):
self.relay.send(self.recv(1024))
def handle_close(self):
print "Closing relay..."
# If the client disconnects, close the
# relay connection as well.
self.relay.close()
self.close()
def readable(self):
return self.is_readable
class RelayServer(asyncore.dispatcher):
def __init__(self, bind_address, MyAppClient_sock):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind(bind_address)
self.MyAppClient_sock = MyAppClient_sock
print self.MyAppClient_sock
self.listen(1)
def handle_accept(self):
conn, addr = self.accept()
RelayClient(self, conn, self.MyAppClient_sock)
if __name__ == "__main__":
# Connect to host
# First connection stage
connectionPhase = 1
c = MyAppClient('host', port) # SomeServer's host and port
asyncore.loop()
編輯:
@samplebias我代替我的完整模塊與您的代碼(未顯示)和我已經重新添加所有位,而我需要進行身份驗證等片
在這一點上,我得到了與上面我自己的代碼相同的結果。我的意思是MyApp(或代碼中的服務器)連接到SomeServer並來回傳遞數據。目前爲止一切都很好。當用戶(或客戶端應用程序)連接到本地主機8080,運行這些代碼:
if not self.listener:
self.listener = Listener(self.listener_addr, self)
,但它沒有運行
# if user is attached, send data
elif self.user:
print 'self.user'
self.user.send(data)
因此,服務器沒有數據中繼到用戶。我在整個User類中添加了打印語句以查看運行的內容,並且init是唯一的。 handle_read()永不運行。
這是爲什麼?
你應該看看[PEP8](http://www.python.org/dev/peps/pep- 0008 /),如果你遵循它,你的代碼不會打破SO的代碼標記;-) – 2011-03-12 00:53:43