Advertisement
Guest User

server.py

a guest
Feb 24th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. import socket
  2. import colorama
  3.  
  4. colorama.init()
  5. hostname = socket.gethostname()
  6. print(socket.gethostbyname(hostname))
  7. input()
  8. LHOST = socket.gethostbyname(hostname)
  9. LPORT = 50000
  10.  
  11. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12. sock.bind((LHOST, LPORT))
  13. sock.listen(1)
  14. print("Listening on port", LPORT)
  15. while True:
  16.     try:
  17.         client, addr = sock.accept()
  18.         break
  19.     except:
  20.         pass
  21. while True:
  22.     input_header = client.recv(1024)
  23.     command = input(input_header.decode()).encode()
  24.  
  25.     if command.decode("utf-8").split(" ")[0] == "download":
  26.         file_name = command.decode("utf-8").split(" ")[1][::-1]
  27.         client.send(command)
  28.         with open(file_name, "wb") as f:
  29.             read_data = client.recv(1024)
  30.             while read_data:
  31.                 f.write(read_data)
  32.                 read_data = client.recv(1024)
  33.                 if read_data == b"DONE":
  34.                     break
  35.  
  36.     if command is b"":
  37.         print("Please enter a command")
  38.     else:
  39.         client.send(command)
  40.         data = client.recv(1024).decode("utf-8")
  41.         if data == "exit":
  42.             print("Terminating connection", addr[0])
  43.             break
  44.         print(data)
  45. client.close()
  46. sock.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement