Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import os,sys
- ss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- ss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- addr=('localhost',4444)
- ss.bind(addr)
- ss.listen(5)
- def main():
- while True:
- try:
- print("Server waiting for connection ..")
- cs,c_addr=ss.accept()
- print("Client connected from:",c_addr)
- file_name=cs.recv(1024).decode()
- if os.path.exists(file_name):
- print("File is in server sending it to client.")
- file=open(file_name,'rb')
- while True:
- data=file.read(4096)
- if not data:
- break
- cs.send(data)
- print("Sending...",end="\r")
- print("Sent.")
- cs.close()
- else:
- print("Receiving file:%s"%file_name)
- #input("Do you want to download it? Press Return to continue or CTRL-D to exit.")
- rcv_file=open(file_name,'wb')
- while True:
- data=cs.recv(4096)
- if not data:
- break
- print("Receiving...",end="\r")
- rcv_file.write(data)
- print("\nRecived")
- cs.close()
- except KeyboardInterrupt:
- ss.close()
- sys.exit(1)
- ss.close()
- main()
Add Comment
Please, Sign In to add comment