Мой код отлично работает в моем IDLE (PyCharm), но когда я открываю файл .exe, появляется ошибка:< /p>
Код: Выделить всё
Traceback (most recent call last):
File "client.py", line 28, in
File "tqdm\std.py", line 1098, in __init__
File "tqdm\std.py", line 1347, in refresh
File "tqdm\std.py", line 1495, in display
File "tqdm\std.py", line 459, in print_status
File "tqdm\std.py", line 452, in fp_write
File "tqdm\utils.py", line 140, in __getattr__
AttributeError: 'NoneType' object has no attribute 'write'
Программа, которую я создал, отправляет файл от клиента к хосту.
client.py:
р>
Код: Выделить всё
import os
import socket
import tqdm
# this pc is the client, we need to write host ip
host = "10.10.10.211"
# same port as in host file
port = 5000
# how many bits to receive each time
BUFFER_SIZE = 4096
# to combine file's name and size while encoding
SEPARATOR = ""
# create the socket
s = socket.socket()
# connecting to the server
print(f"[+] Connecting to {host}:{port}")
s.connect((host, port))
print("[+] Connected.")
file_name = r"C:\Users\danus\OneDrive\Pictures\Screenshots\Screenshot 2024-02-20 185759.png"
file_size = os.path.getsize(file_name)
# sending the info about the file through the socket
s.send(f"{file_name}{SEPARATOR}{file_size}".encode())
# progress bar
progress = tqdm.tqdm(range(file_size),
f"Sending {file_name}",
unit="B",
unit_scale=True,
unit_divisor=1024)
# sending the file using 'read bytes' mode
with open(file_name, "rb") as file:
while True:
# read the bytes from the file
bytes_read = file.read(BUFFER_SIZE)
if not bytes_read:
# file transmitting is done
break
# else send the bytes that were just read
s.sendall(bytes_read)
# update the progress bar
progress.update(len(bytes_read))
# close the socket
s.close()
Код: Выделить всё
import os
import socket
import tqdm
# this pc is the host, so we can receive files to all ips
SERVER_HOST = "0.0.0.0"
# make a port (any numbers) which will connect the devices
SERVER_PORT = 5000
# how many bits to send each time
BUFFER_SIZE = 4096
# to separate file's name and size after the encoding
SEPARATOR = ""
# create the socket
s = socket.socket()
# bind the socket to our local address
s.bind((SERVER_HOST, SERVER_PORT))
# enabling our server to accept connections
s.listen(5)
print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}")
# accept connection if there is any
client_socket, address = s.accept()
print(f"[+] {address} is connected.")
# receiving the info using client socket
received = client_socket.recv(BUFFER_SIZE).decode()
file_name, file_size = received.split(SEPARATOR)
file_size = int(file_size)
# remove absolute path if there is
file_name = os.path.basename(file_name)
# progress bar
progress = tqdm.tqdm(range(file_size),
f"Receiving {file_name}",
unit="B",
unit_scale=True,
unit_divisor=1024)
# writing the file using 'write bytes' mode
with open(file_name, mode="wb") as file:
while True:
# read the bytes from the file
bytes_read = client_socket.recv(BUFFER_SIZE)
if not bytes_read:
# file transmitting is done
break
# else write to the file bytes that were just received
file.write(bytes_read)
# update the progress bar
progress.update(len(bytes_read))
# close the client socket
client_socket.close()
# close the server socket
s.close()
Буду благодарен за помощь.
Спасибо< /п>
Подробнее здесь: https://stackoverflow.com/questions/793 ... ing-an-exe
Мобильная версия