服务端:
import socket
import threading
import tkinter
def run ( ) :
global window
window = tkinter. Tk( )
window. geometry( '300x100' )
tkinter. Button( window, text= "截图" , width= 15 , height= 2 , command= send) . pack( )
window. mainloop( )
def send ( ) :
clientsocket. send( "screen" . encode( 'utf-8' ) )
if __name__== '__main__' :
global clientsocket
threading. Thread( target= run) . start( )
serversocket = socket. socket( socket. AF_INET, socket. SOCK_STREAM)
serversocket. bind( ( socket. gethostname( ) , 8090 ) )
serversocket. listen( 5 )
while True :
clientsocket, addr= serversocket. accept( )
myrecv = clientsocket. recv( 1024 )
print ( myrecv. decode( 'utf-8' ) )
msg= '连接成功!'
clientsocket. send( msg. encode( 'utf-8' ) )
clientsocket. close( )
客户端:
import socket
import time
from PIL import ImageGrab
import paramiko
import os
def connect ( ) :
client = socket. socket( socket. AF_INET, socket. SOCK_STREAM)
try :
client. connect( ( socket. gethostname( ) , 8090 ) )
except :
pass
return client
if __name__== '__main__' :
client = connect( )
client. send( "请求连接!" . encode( 'utf-8' ) )
while True :
try :
msg = client. recv( 1024 )
event = msg. decode( 'utf-8' )
if event == 'screen' :
pic = ImageGrab. grab( 0 )
pic_name = time. strftime( "%Y-%m-%d-%H-%M-%S" , time. localtime( ) ) + '.jpg'
pic. mode = 'RGB'
pic. save( pic_name)
ssh = paramiko. SSHClient( )
ssh. set_missing_host_key_policy( paramiko. AutoAddPolicy( ) )
ssh. connect( "127.0.0.1" , 22 , "root" , "123456" )
sftp = ssh. open_sftp( )
sftp. put( pic_name, "/www/wwwroot/images/" + pic_name)
os. remove( pic_name)
print ( "发送完成" )
else :
print ( event)
except socket. error:
client = connect( )