Python如何使插座服务器使用客户端内置的命令



我有客户端和服务器,如果客户端具有命令,该命令只是打印出hello hello world,我将如何运行该命令,但在服务器代码中?Hello World代码应该像这样

if data == "hello":
  print("Hello World!")

对不起,如果您不了解我的问题,我不擅长解释事情

带有您给我的信息,没有代码,这是我可以提出的最好的:

服务器代码:

# client gives data to server    
if data == "hello":
      return "func" # use whatever method you already do to send this as a response to the user

客户端代码

import sys
# ...
def func(): # note this is the same name as what the server returns
    print("Hello world!")
# ...
response = <response from server> # you should know how it gets this, but this should (in this example) be 'func'
this_module = sys.modules[__name__]
getattr(this_module, response)() # this line executes the function, and will print "Hello world!"

我希望这会有所帮助!

最新更新