Python Basic -Socket编程远程执行命令后将结果返回请求者

前提1:subprocesses模块的使用方法

subprocesses模块的使用方法请点击

前提2:socket网络编程

示例(不完美示例,仅功能性展示)

在前提2中已经完成了可以让两台主机通过socket进行对话,现在我们让客户端连接到服务器上,在服务器上执行命令,然后服务器将命令执行完成后将结果发回给客户端。以下为相关代码:

服务端代码:

import socket
import subprocess
sk = socket.socket()
address = ("127.0.0.1",9200)
sk.bind(address)
sk.listen(3)
print("waiting.....")
connection,clientinfo = sk.accept()
print(connection)
print(clientinfo)


while True:
    try:
        print("waiting for message input.....")
        user_recv = connection.recv(1024)
    except ConnectionResetError as e:
        connection.close()
        print(e)
        print(clientinfo,"已经主动关闭了会话")
        connection, clientinfo = sk.accept()#因为上面对connection关闭了,所以这里需要重新来一个connection等待连接
        continue
    if not user_recv:# 当用户输入exit的时候,服务端会接收到一个“空”的内容,所以判断用户终止了会话,所以我们接收一个新的连接。
        print("当前用户已经退出会话。")
        connection.close()
        connection, clientinfo = sk.accept()
        print("新用户建立了一个连接,客户端信息:",clientinfo)
        continue
    command = str(user_recv,"utf8")
    command_result = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    command_result_str = command_result.stdout.read()
    #计算出数据有多大
    result_length = len(command_result_str)
    print("发送数据大小为:",result_length)



    try:
        connection.sendall(command_result_str)
        print("运行结果已经发送")
    except ConnectionResetError as e:
        connection.close()
        print(e)
        print(clientinfo,"已经主动关闭了会话")
        connection, clientinfo = sk.accept()#因为上面对connection关闭了,所以这里需要重新来一个connection等待连接
        continue

connection.close()

客户端代码

import socket

sk = socket.socket()
address = ("127.0.0.1",9200)
sk.connect(address)


while True:
    user_input = input(">>>")
    if user_input == "exit":
        print("exiting....")
        break


    sk.send(bytes(user_input,"utf8"))


    user_recv = sk.recv(1024)
    print("接收数据完成")
    print(str(user_recv,"utf8"))
sk.close()

运行并展示结果

服务端执行

waiting.....
<socket.socket fd=640, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 9200), raddr=('127.0.0.1', 62804)>
('127.0.0.1', 62804)
waiting for message input.....
运行结果已经发送
waiting for message input.....

客户端

>>>dir
接收数据完成
 Volume in drive E is New Volume
 Volume Serial Number is 1096-52E4

 Directory of E:\Nextcloud\NAS\Study\Study-Note\Python\Pycharm\Lab\week11

2020/09/26  20:05    <DIR>          .
2020/09/26  20:05    <DIR>          ..
2020/09/26  20:05               506 Clientcommand.py
2020/09/26  20:05             1,752 Servercommand.py
2020/09/25  11:28               364 socketclient.py
2020/09/25  15:00             1,554 sockettest.py
2020/09/25  17:19               729 testsubprocesses.py
2020/09/23  21:17    <DIR>          __pycache__
               5 File(s)          4,905 bytes
               3 Dir(s)  72,243,949,568 bytes free

>>>

== 不完美之一,只能接收1024个字节,所以需要改成sendall,且接收也得改大点。但是无论改多大都有可能超过限值的。如何能够长远的更改呢?如果服务器是sendall,那服务器肯定sendall发送完了,但是接收的限值,所以需要使用一个循环来一直接收数据,直到收完

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值