Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # server.py
- import socket # Import socket module
- import ssl
- import sys # Import system-specific parameters and functions
- import hashlib
- import os.path # Import module to manipulate paths
- import os, time, _thread as thread
- from threading import Thread
- from SocketServer import ThreadingMixIn
- socket.gethostbyname
- TCP_IP = 'localhost'
- TCP_PORT = 9001
- BUFFER_SIZE = 1024
- def now():
- return time.asctime()
- def myrec(the_socket):
- total_data=[]
- while True:
- data = the_socket.recv(8192)
- if not data: break
- total_data.append(data)
- return ''.join(total_data)
- class ClientThread(Thread):
- def __init__(self,ip,port,sock):
- Thread.__init__(self)
- self.ip = ip
- self.port = port
- self.sock = sock
- print " New thread started for "+ip+":"+str(port)
- def run(self):
- user_names = []
- passwords = []
- #user_names, passwords = process_file("pass.txt")
- user = self.sock.recv(1024) # Get up to 1024 bytes at a time
- print "User: ", user
- try:
- file_conn = open("pass.txt")
- data = file_conn.readlines()
- for i in range(len(data)):
- if i%2 == 0:
- user_names.append(data[i][:-1])
- else:
- passwords.append(data[i][:-1])
- file_conn.close()
- except:
- sys.exit('There was a problem reading the file!')
- if user not in user_names:
- self.sock.send ("Unkown User Name, terminating... ")
- sys.exit('Unkown User Name, terminating... n')
- else:
- self.sock.send ("Please enter password: ")
- password = self.sock.recv(1024) # Get up to 1024 bytes at a time
- print "Password sent to server." + password
- user_input = hashlib.sha224(password).hexdigest()
- dir_name = ((r'/home/%s') % (user))
- os.chdir(dir_name)
- if user_input != passwords[user_names.index(user)]:
- print 'Incorrect Password!n'
- self.sock.send ("Incorrect password. ")
- sys.exit('Incorrect Password, terminating... n')
- else:
- print 'User is logged in!n'
- self.sock.send ('User is logged in!n')
- filename='se.py'
- f = open(filename,'rb')
- while True:
- l = f.read(BUFFER_SIZE)
- while (l):
- self.sock.send(l)
- #print('Sent ',repr(l))
- l = f.read(BUFFER_SIZE)
- if not l:
- f.close()
- self.sock.close()
- self.sock.close()
- tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- tcpsock.bind((TCP_IP, TCP_PORT))
- threads = []
- while True:
- tcpsock.listen(5)
- print "Waiting for incoming connections..."
- (conn, (ip,port)) = tcpsock.accept()
- print 'Got connection from ', (ip,port)
- newthread = ClientThread(ip,port,conn)
- newthread.start()
- threads.append(newthread)
- for t in threads:
- t.join()
- # client2.py
- #!/usr/bin/env python
- import socket # Import socket module
- import sys # Import system-specific parameters and functions
- import os.path # Import module to manipulate paths
- import ssl
- import pprint
- TCP_IP = 'localhost'
- TCP_PORT = 9001
- BUFFER_SIZE = 1024
- def myrec(the_socket):
- total_data=[]
- while True:
- data = the_socket.recv(8192)
- if not data: break
- total_data.append(data)
- return ''.join(total_data)
- print 'nUser & Password Authenticationn'
- ssl_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- ssl_sock.connect((TCP_IP, TCP_PORT))
- print 'file opened'
- user = raw_input('Please Enter User Name: ')
- print 'name send to server: ' + user
- ssl_sock.send(user)
- data = ssl_sock.recv(1024)
- print data
- if data == "Unkown User Name, terminating... ":
- sys.exit('Unknown user name, terminating... n')
- else:
- # data = ssl_sock.recv(2048)
- password= raw_input(data)
- print 'pas send to server: ' + password
- ssl_sock.send(password)
- data = ssl_sock.recv(2048)
- if data == "User is logged in!n":
- dir_name = ((r'/home/client/%s') % (user))
- os.chdir(dir_name)
- print 'Client is logged in!n'
- with open('received_file', 'wb') as f:
- print 'file opened'
- while True:
- #print('receiving data...')
- data = ssl_sock.recv(BUFFER_SIZE)
- print('data=%s', (data))
- if not data:
- f.close()
- print 'file close()'
- break
- # write data to a file
- f.write(data)
- # print "exit client"
- elif data == "Incorrect password. ":
- # print 'Incorrect password. n'
- sys.exit('Incorrect password. n')
- else:
- print "Terminating... n"
- print('Successfully get the file')
- ssl_sock.close()
- print('connection closed')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement