change ip to 127.0.0.1

This commit is contained in:
CactiChameleon9 2020-12-10 08:00:26 +00:00
parent a0010092ff
commit bd417edb89

View File

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