我有客户端和服务器,如果客户端有一个命令,比如说只是打印出 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!"
我希望这有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句