Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket # Import socket module
- from pathlib import Path
- import time
- port = 8080 # Reserve a port for your service every new transfer wants a new port or you must wait.
- s = socket.socket() # Create a socket object
- host = "" # Get local machine name
- s.bind((host, port)) # Bind to the port
- s.listen(5) # Now wait for client connection.
- print('Listening...')
- while True:
- conn, addr = s.accept() # Establish connection with client.
- print('New connection!', addr)
- filename = conn.recv(256).decode()
- print('Server received', filename)
- time.sleep(2)
- file_check = Path('received/' + filename)
- if not file_check.is_file():
- conn.send(b'Got it, saving file as ' + bytes(filename, encoding='utf8'))
- print('msg sent')
- file = open('received/' + filename, 'wb')
- while True:
- buff = conn.recv(2)
- if buff.decode() == 'ok':
- conn.close()
- break
- buff = conn.recv(1024)
- file.write(buff)
- file.close()
- else:
- print('file found')
- i = 1
- filename = filename.split('.')
- name = filename[0] + '_copy' + str(i) + '.' + '.'.join(str(x) for x in filename[1:])
- while Path('received/' + name).is_file():
- i += 1
- name = filename[0] + '_copy' + str(i) + '.' + '.'.join(str(x) for x in filename[1:])
- conn.send(b'That file already exists. Saving as ' + bytes(name, encoding='utf8'))
- file = open('received/' + name, 'wb')
- while True:
- buff = conn.recv(2)
- if buff == b'ok' or buff == b'':
- conn.close()
- break
- buff = conn.recv(1024)
- file.write(buff)
- file.close()
- print('Done receiving')
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement