Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######## SERVIDOR ########
- import socket, json
- def get_file(conn, info):
- remaining = info['size']
- file_bin = b''
- while remaining > 0:
- try:
- package = conn.recv(2048)
- except Exception as err:
- return None
- file_bin += package
- remaining -= len(package)
- return file_bin
- host, port = ('', 9005)
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
- sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- sock.bind((host, port))
- sock.listen(5)
- while True:
- conn, addr = sock.accept()
- print('conn', addr)
- info_bin = b''
- while True:
- c = conn.recv(1)
- if c == b'\x00':
- break
- info_bin += c
- info = json.loads(info_bin.decode())
- file_bin = get_file(conn, info)
- if file_bin is not None:
- dest = 'files/{}'.format(info['name'])
- with open(dest, 'wb') as f:
- f.write(file_bin)
- print('success on receiving and saving {} for {}'.format(info['name'], conn.getpeername()))
- conn.close()
- ########## CLIENTE ###########
- import socket, json
- f = input('escreva o nome do ficheiro')
- f_bin = open(f, 'rb').read()
- info = {'name': f, 'size': len(f_bin)}
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
- s.connect(('89.115.59.255', 8080))
- s.send(json.dumps(info).encode())
- s.send(b'\x00') # byte nulo para sabermos do lado servidor que ja recebemos a informcao
- total_sent = 0
- s.sendall(f_bin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement