Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. SERVER
  2. import socket
  3. import hashlib
  4.  
  5. def PWcheck():
  6. global data
  7. f= open("passwords.txt")
  8. f1=f.readlines()
  9. if password in f1:
  10. print("Login successfull")
  11. ls = ("login successfull")
  12. c.send(ls.encode())
  13.  
  14. elif password != f1:
  15. print("Password incorrect")
  16. ls = ("Password incorrect")
  17. c.send(ls.encode())
  18. recive()
  19.  
  20. def recive():
  21. global password
  22. password = c.recv(1024).decode()#reciving the password
  23. print("Password from client: ", password)
  24. PWcheck()
  25.  
  26. def menu():
  27. message = ("Welcome to the system. Press 1 to register, 2 to login.")
  28. c.send(message.encode())
  29. recive()
  30.  
  31. def listening():
  32. print("Listening")
  33. global data
  34. data = s.recv(1024).decode()
  35.  
  36. def sending(text):
  37. global data
  38. data = c.sendall(text.encode())
  39.  
  40. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  41. print("Listening: ")
  42. s.bind(('127.0.0.1', 4000))
  43. s.listen(10) #listen to 1 connection at a time
  44. c, addr = s.accept()
  45. print ("Connection from: ", addr)
  46.  
  47. menu()
  48.  
  49.  
  50. c.close()
  51. input("\n\n press enter to close")
  52. #########################################################################
  53. CLIENT
  54. import socket
  55. import hashlib
  56. import os
  57. import os, sys
  58. from stat import *
  59. cwd = os.getcwd() #current working directory
  60.  
  61. def hashPW(password): #take a password, hashing it then saving it in a database
  62. return hashlib.sha256(str.encode(password)).hexdigest()
  63.  
  64. def register():
  65. password = str(input("Please choose a password: "))
  66. message = hashPW(password)
  67. print("Password created.")
  68. f= open("passwords.txt","a")#creates a file for the password
  69. f.write('\n'+ message)
  70. f.close()
  71. login()
  72.  
  73. def login():
  74. password = str(input("Please enter your password to login: "))
  75. message = hashPW(password)
  76. s.send(message.encode())
  77. ls = (s.recv(1024))
  78. if ls ==("login successfull"):
  79. print(ls)
  80. select()
  81. else:
  82. print(ls)
  83. login()
  84.  
  85.  
  86. def select():
  87. select = s.recv(1024)
  88. print(select)
  89. if select ==1:
  90. file_check()
  91. elif select ==2:
  92. email()
  93.  
  94.  
  95. def email_message():
  96. print("this will be email once ian gets to it")
  97.  
  98. def file_write():
  99. pass
  100.  
  101. def file_check():
  102. print("Current directory: ", os.getcwd())
  103. print("")
  104. print("Type ", '{:<30}'.format("File name"), "Size of file (Bytes)")
  105. print("---- ", '{:<30}'.format("---------"), "-"*20)
  106. for file in os.listdir("."):
  107. # get details of file
  108. try:
  109. a=os.stat(file)
  110. if( S_ISDIR(a.st_mode) ):
  111. print("<dir> ", file)
  112. else:
  113. print("<file> ", '{:<30}'.format(file), a.st_size)
  114. except:
  115. print(file, " <<unable to access>>")
  116.  
  117. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  118.  
  119. print("Waiting for connection")
  120. s.connect(('127.0.0.1', 4000))
  121.  
  122. data = s.recv(1024).decode()#max number of bytes recv
  123. print (data)
  124. choice = int(input("Please input your choice"))
  125.  
  126.  
  127. if choice == 1:
  128. register()
  129. elif choice == 2:
  130. login()
  131. elif choice == 3:
  132. file_check()
  133.  
  134. s.close()
  135. input("\n\n press enter to close")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement