gpt4 book ai didi

python - 需要帮助在两个套接字之间创建 TCP 中继

转载 作者:太空宇宙 更新时间:2023-11-03 15:27:07 25 4
gpt4 key购买 nike

我有以下情况:

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)User 连接。 User 连接后,MyApp 将数据从 SomeServer 中继到 User。这发生在两个方向。

我有 SomeServer(S) <-> (C)MyApp 完美运行,但我无法获得 MyApp(S) <-> (C)User 工作。我最远到达 User 连接到 MyApp(S),但无法中继数据!

好的,我希望我已经清楚了 ;) 现在让我展示我的 MyApp 代码。顺便说一句,SomeServerUser 的实现与解决我的问题相关,因为两者都无法修改。

我已经评论了我的代码,指出了我遇到问题的地方。哦,我还应该提到,如有必要,我可以毫无问题地为其他一些代码废弃整个“服务器部分”。这是一个 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(或您代码中的 Server)连接到 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() 永远不会运行。

这是为什么?

最佳答案

代码有点难理解,我敢肯定有一些错误。为了handle_read() 中的示例,您将 MyAppClient 的原始套接字 self.socket 传递给中继服务器。您最终会在同一个套接字上工作 MyAppClient 和 RelayConnection。

与其尝试对我放在一起的原始代码提出错误修复建议一个执行您的代码意图并且更清晰且更易于遵循的示例。我已经测试过它与 IMAP 服务器的对话并且它可以工作,但省略了一些为简洁起见(错误处理、在所有情况下正确的 close() 处理等)。

  • Server 启动与“someserver”的连接。一旦连接它启动Listener
  • Listener 监听端口 8080 并只接受 1 个连接,创建一个 User,并将其传递给 Server 的引用。 Listener 拒绝所有其他用户处于事件状态时的客户端连接。
  • 用户将所有数据转发给服务器,反之亦然。评论指示身份验证应插入的位置。

来源:

import asyncore
import socket

class User(asyncore.dispatcher_with_send):

def __init__(self, sock, server):
asyncore.dispatcher_with_send.__init__(self, sock)
self.server = server

def handle_read(self):
data = self.recv(4096)
# parse User auth protocol here, authenticate, set phase flag, etc.
# if authenticated, send data to server
if self.server:
self.server.send(data)

def handle_close(self):
if self.server:
self.server.close()
self.close()

class Listener(asyncore.dispatcher_with_send):

def __init__(self, listener_addr, server):
asyncore.dispatcher_with_send.__init__(self)
self.server = server
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(listener_addr)
self.listen(1)

def handle_accept(self):
conn, addr = self.accept()
# this listener only accepts 1 client. while it is serving 1 client
# it will reject all other clients.
if not self.server.user:
self.server.user = User(conn, self.server)
else:
conn.close()

class Server(asyncore.dispatcher_with_send):

def __init__(self, server_addr, listener_addr):
asyncore.dispatcher_with_send.__init__(self)
self.server_addr = server_addr
self.listener_addr = listener_addr
self.listener = None
self.user = None

def start(self):
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect(self.server_addr)

def handle_error(self, *n):
self.close()

def handle_read(self):
data = self.recv(4096)
# parse SomeServer auth protocol here, set phase flag, etc.
if not self.listener:
self.listener = Listener(self.listener_addr, self)
# if user is attached, send data
elif self.user:
self.user.send(data)

def handle_close(self):
if self.user:
self.user.server = None
self.user.close()
self.user = None
if self.listener:
self.listener.close()
self.listener = None
self.close()
self.start()

if __name__ == '__main__':
app = Server(('someserver', 143), ('localhost', 8080))
app.start()
asyncore.loop()

关于python - 需要帮助在两个套接字之间创建 TCP 中继,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5279641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com