如何捕获并发送返回'在python中远程执行的函数的值

发布于 2025-01-31 10:41:03 字数 2326 浏览 3 评论 0原文

我正在尝试开发一个客户端/服务器脚本来在远程计算机上执行一些Python代码(为了远程或远程控制某些设备。)

我能够远程通信和执行命令(我在服务器端列出的虚拟命令。 )

我一直在尝试向发送给服务器的命令的输出发送给客户端的输出的反馈,但是看来我无法抓住它:

Test client sending packets to IP 131.169.33.198, via port 5004

b'Dummy.one(1,2)'
b'Dummy.two(1)'
b'Dummy.three(1,2)'

因此,如果有人可以告诉我如何捕获我如何捕获的返回值,我将不胜感激该函数远程执行,以将其发送回客户端。到目前为止,当服务器捕获我发送并执行的三个命令时,我会从print(命令)中获得以下结果:

Test server listening on port 5004

None
None
None

我在下面的服务器和客户端提供了我的MWE脚本执行毫无例外。

最后一个问题将是如何在退出服务器之前有效关闭端口,以便我找不到有关端口状态的错误消息。

预先感谢

服务器脚本

# Server code 

# -*- coding: utf-8 -*-
"""
Created on Fri May 20 11:40:03 2022

@author: strelok
"""

from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
import os
import time

class Dummy:
    
    def one(arg1,arg2):
        a = f"I got {arg1} and {arg2}"
        return a

    def two(arg1):
        a = f"I got {arg1}"
        # print(a)
        return a
        
    def three(arg1,arg2):
        a = arg1 + arg2
        # print(f"the answer is {a}")
        return a

PORT_NUMBER = 5004
SIZE = 1024

hostName = gethostbyname( '0.0.0.0' )

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )

print ("Test server listening on port {0}\n".format(PORT_NUMBER))

while True:
    (data,addr) = mySocket.recvfrom(SIZE)   
    command = exec(str(data, encoding = 'utf-8'))
    print(command)
    # mySocket.send(bytes(command))
     

sys.exit()

客户端脚本

# Client code
# -*- coding: utf-8 -*-
"""
Created on Fri May 20 11:34:32 2022

@author: strelok
"""

import sys
from socket import socket, AF_INET, SOCK_DGRAM
import time

def send_command(text):
        message = bytes(text, encoding = 'utf-8')
        print(message)
        mySocket.send(message)
        time.sleep(.5)
        

SERVER_IP   = '131.169.33.198'
PORT_NUMBER = 5004
SIZE = 1024
print ("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.connect((SERVER_IP,PORT_NUMBER))

# Sending the commands to be executed remotely

send_command("Dummy.one(1,2)")
send_command("Dummy.two(1)")    
send_command("Dummy.three(1,2)")

sys.exit()

I am trying to develop a client/server script to execute some Python code on a remote computer (for the sake or controlling some devices remotely.)

I am able to communicate and execute commands remotely (the dummy commands I listed on the server side.)

I have been trying to send feedback on the output of the commands sent to the server back to the client, but it seems I am unable to catch it:

Test client sending packets to IP 131.169.33.198, via port 5004

b'Dummy.one(1,2)'
b'Dummy.two(1)'
b'Dummy.three(1,2)'

so I would be grateful if someone could tell me how to catch the return values of the function executed remotely in order to send it back to the client. So far, when the server catches the three commands I send and executes them, I get the following result from print(command):

Test server listening on port 5004

None
None
None

I am providing with my MWE scripts below for both server and client - they should execute without exceptions.

One last question would be on how to efficiently close the ports before exiting the server, so that I do not find error messages regarding the port status.

Thanks in advance

Server script

# Server code 

# -*- coding: utf-8 -*-
"""
Created on Fri May 20 11:40:03 2022

@author: strelok
"""

from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
import os
import time

class Dummy:
    
    def one(arg1,arg2):
        a = f"I got {arg1} and {arg2}"
        return a

    def two(arg1):
        a = f"I got {arg1}"
        # print(a)
        return a
        
    def three(arg1,arg2):
        a = arg1 + arg2
        # print(f"the answer is {a}")
        return a

PORT_NUMBER = 5004
SIZE = 1024

hostName = gethostbyname( '0.0.0.0' )

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )

print ("Test server listening on port {0}\n".format(PORT_NUMBER))

while True:
    (data,addr) = mySocket.recvfrom(SIZE)   
    command = exec(str(data, encoding = 'utf-8'))
    print(command)
    # mySocket.send(bytes(command))
     

sys.exit()

Client script

# Client code
# -*- coding: utf-8 -*-
"""
Created on Fri May 20 11:34:32 2022

@author: strelok
"""

import sys
from socket import socket, AF_INET, SOCK_DGRAM
import time

def send_command(text):
        message = bytes(text, encoding = 'utf-8')
        print(message)
        mySocket.send(message)
        time.sleep(.5)
        

SERVER_IP   = '131.169.33.198'
PORT_NUMBER = 5004
SIZE = 1024
print ("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.connect((SERVER_IP,PORT_NUMBER))

# Sending the commands to be executed remotely

send_command("Dummy.one(1,2)")
send_command("Dummy.two(1)")    
send_command("Dummy.three(1,2)")

sys.exit()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文