you are required to define separate functions of your own to calculate individually and return the statistical measures of mean, minimum and maximum. In other words, you are not allowed to use any built-in, library or any other third-party functions to accomplish the task.
Answer:
Analysis and Design:
The project is a Socket Programming (Network Programming) in python which demonstrate the client-server architecture of the project
In this project there are two components: SERVER and CLIENT.
Server program runs first and it waits for clients to get connected. Once a client get connected it displays the client IP address and port number of client. The server program then authenticate the user name and password which is entered in the client program, by checking the users.txt file which stores the record of all the user. If the user name and password is available, and authentication is successful, it sends the message “Logged in successfully” to the client where this message is displayed. And if the authentication failed then it displays the message “Username not available” if the inputted user name is not available in the user.txt file or “User already logged in” if the user is already to the server. The server keeps a list of all the logged user for this purpose where once the user is logged in successfully, the username of that user is stored in the logged user list.
Pseudocode for login :
-
- Input usrname and paswd in client program
- Send usrname and paswd to server program
- Read “users.txt” file and load all users in userlist
- For usr in userlist
- Check if usrname is present in loggeduser list
- If found set msg = “User already logged in”
- Exit for loop and go to step 5
- Check usrname and paswd present in userlist
- If found set msg = “Logged in successfully”
- Exit for loop and go to step 5
- Else set msg = “Username not available” and exit loop
- Check if usrname is present in loggeduser list
- Send msg to client program
- Display the msg
Server code for login procedure:
Client code for login procedure :
Once the client have logged in successfully, it displays a menu with six options :
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
In option 1, we input the organization name, which is sent to the server. In the server program, it execute a method where it iterate the list of organization records and checks if the organization name is there in the record. If the name exist, it sends back the domain name and IP address of that record back to the client program. If the organization name is not available in the organization list, it send back a message that “Orgnaizartion name is unknown”, which is displayed in the client program
Pseudocode for option 1 :
-
- Input orgname in client program
- Send orgname to server program
- For org in orglist
- Check if orgname in org
- Set outdata = domname + “ “ + ip
- Send outdata to client program
- Check if orgname in org
-
- Exit loop and go to step 4
-
- Else
- Set outdata = “organization is unknown”
- Go to step 1
- Receive outdata from server
- Print outdata
- Else
The server and client part of coding for this option 1 :
Server code :
Client code :
In option 2, the Server program executes methods calcminimum(), calcmaximum() and calcmean() to calculate and find the minimum, maximum and mean value of the time from the organization list and send back these three values to the client program where they are displayed on the screen.
Pseudocode for option 2 :
-
- Call method calcminimum() in server to find the minimum value of minutes
- For org in orglist
- Add minutes field to minminutes list
- Set mindata = minminutes[0]
- For x = 0 to len(minminutes)
- If minminutes[x] < mindata
- Set mindata = minminutes[x]
- Return mindata
- If minminutes[x] < mindata
- Call method calcmaximum in server to find the maximum values of minutes
- For org in orglist
- Add minutes field to maxminutes list
- Set maxdata = maxminutes[0]
- For x = 0 to len(maxminutes)
- If maxminutes[x] > maxdata
- Set maxdata = maxminutes[x]
- Return maxdata
- If maxminutes[x] > maxdata
- Call method calcmean() in server to find the mean value of minutes
- For org in orglist
- Add minutes field to meanminutes list
- Set total = 0
- For x = 0 to len(meanminutes)
- Total = total + meanminutes[x]
- Mean = total / len(meanminutes)
- Return mean
- For org in orglist
- Send mindata, maxdata, mean to client
- Receive mindata, maxdata and mean from server
- Print mindata, maxdata and mean values
- For x = 0 to len(maxminutes)
- For org in orglist
- For x = 0 to len(minminutes)
- For org in orglist
- Call method calcminimum() in server to find the minimum value of minutes
The server and client code for this option 2 :
Server code :
Client Code :
In option 3, the organization list is sorted either on the basis of organization name is ascending order or on the basis of time in minutes in descending order.
Pseudocode for option 3 :
-
- Input sorting order choice in client (1 or 2)
- Send the sort choice to server
- If choice = 1
- Sort orglist in ascending order of organization name
- For org in orglist
- Concatenate the organization data to outdata variable
- Send the outdata to client program
- If choice = 2
- Sort orglist in descending ord
er of minutes - For org in orglist
- Concatenate the organization data to outdata variable
- Send the outdata to client program
- Receive outdata value from server
- Print outdata
- Sort orglist in descending ord
The server and client code for option 3 :
Server Code :
Client code :
In option 4, we are able to store the record of new organization in the organization.txt file. In this option, first we enter organization name, domain name, IP address and time in minutes in the client side. This information is then sent to the server program. In the server program, first we check whether the organization name is already available in the organization list. If the organization name is not there in the organization list, then this new record is added to the organization list which also get updated in the organization.txt file and message is send to the client program “Organization data saved successfully” and if the organization name is available in the organization list, then it displays the message “Organization already exists”. The user is able to enter the new organization record for 3 times and after that the program terminates if the existing organization name is entered 3 times
Pseudocode for option 4 :
-
- Input orgname, domname, ipadr, minu in client
- Send orgname, domname, ipadr, minu to server
- For org in orglist
- Check if orgname in org
- Set status = true
- Exit for loop
- Set status = false
- Exit for loop
- If status = false
- add organization data to orglist
- open “organizations.txt” file for writing
- update the file with new record
- close file
- send status to client
- receive status from server
- if status = true
- print “organization data save successfully
- exit
- else
- print “organization already exist”
- go to step 1
The server and client code for this option 4 :
Server code :
Client code :
In option 5, we are able to remove the record of organization from the organizations.txt file. First we input the name of the organization name in the client part, the organization name is then send to the server program, where it checks whether the organization name is available in the organization list. If name is available it removes the organization record from the list and also update the organizations.txt file by removing the organization record from the file. On the contrary if the organization name is not available in the organization list, the server program send back a message “Organization does not exists” which is displayed in the client program.
Pseudocode for option 5 :
-
- Input organization name, orgname in client program
- Send orgname to server program
- Set status = false
- For org in orglist
- Check if orgname is present in org
- If found, remove org from orglist
- Set status = true
- Check if orgname is present in org
-
- Exit for loop
-
- If status = true
- Open “organizations.txt” file for writing
- Update the file with updated orglist and removing the deleted organization record
- Close file
- Send status to client program
- Receive status from server program
- If status is true
- Print “Organization removed successfully”
- Else if status is false
- Print “Organization does not exists”
- If status = true
The server and client code for option 5 :
Server code :
Client code :
The option 6 is used to terminate the program by displaying a message
FLOWCHART :
SERVER PROGRAM FLOWCHART :
CLIENT PROGRAM FLOWCHART :
Program Coding and Output Screens :
SERVER PROGRAM CODING :
import socket # import the socket package to create server and client sockets
import sys
loggedusr = [] # list of the logged user
users = [] #list of all the users
orglist = [] # list of the organization records
domain = ''
ip = ''
q = 0
minminutes = [] # list to minutes to find the minimum, maximum and mean value respectively
maxminutes = []
meanminutes = []
def getkeyname(namelist):
return namelist[0]
def getkeysize(sizelist):
return sizelist[3]
def calcminimum(): # method to find the minimum of the minutes
for org in orglist: # loop the organization list
fields = org.strip().split(' ') # split the record
minminutes.append(int(fields[3])) # store the minutes value in minminutes list
mindata = minminutes[0] # store the minutes of index 0 to mindata
for x in range(0, len(minminutes)): # loop through the minminutes list
if minminutes[x] < mindata: # check if the current minminutes value is < mindata
mindata = minminutes[x] # if true store the current value to mindata
return str(mindata) # return back the mindata
def calcmaximum(): # method to find the maximum of the minutes
for org in orglist: # loop the organization list
fields = org.strip().split(' ') # split the record
maxminutes.append(int(fields[3])) # store the minutes value in maxminutes list
maxdata = maxminutes[0] # store the minutes of index 0 to maxdata
for x in range(0, len(maxminutes)): # loop through the maxminutes list
if maxminutes[x] > maxdata: # check if the current value is > maxdata
maxdata = maxminutes[x] # if true store the current value to maxdata
return str(maxdata) # return back the maxdata
def calcmean(): # method to find the mean value of the minutes
for org in orglist: #loop through the organization list
fields = org.strip().split(' ') # split the record
meanminutes.append(int(fields[3])) # store the minutes value to meanminutes list
total = 0 # initialize the total variable to 0
for x in range(0, len(meanminutes)): # loop through the meanminutes list
total = total + meanminutes[x] # add all the minutes to total variable
avg = int(total / len(meanminutes)) # find the mean value by dividing the total by the length of meanmiutes list
return str(avg) # return back the mean value
def loadorg(): # method to read the organizations.txt file and store the organization record to orglist
fr1 = open('organisations.txt', 'r') # open file for reading
lines = fr1.readlines() # read all the records/lines and store it in lines list
for l in lines: # loop through all the lines
orglist.append(l) # store each organization record to orglist
fr1.close() # close the file
def domainip(orgname): # method to find the domain name and ip address of the given organization name
fields =[]
q = 0
for org in orglist: # loop through the organisation list
if orgname in org: #check if the orgname is there in the current organisation record
fields = org.strip().split(' ') # if true, split the record
global domain
global ip
domain = fields[1] # store the domain value
ip = fields[2] # store the ip address value
q = 1
break
return q
def loggeduser(usrname, pswd): # method to check whether the user is already logged in or not
found = False
for usr in loggedusr: # loop through the logged user list
if usrname + ' ' + pswd in usr: # check if the given username and password is there in current record
found = True # if true set found to true
break
return found # return back the found value
def login(usrname, pswd): # method to login the user with given username and password to the program
fr = open('users.txt', 'r') # open users.txt file for reading
users = fr.readlines() # read all the records of users and store it in users list
found = False
msg = ''
for user in users: # loop through the users list
status = loggeduser(usrname, pswd) # call the loggeduser method to check whether the user is already logged in or not
if status == True: # if status is true, then it means the user is already logged in
found = True
msg = 'User already logged in'
break
else:
if usrname + ' ' + pswd in user: #check if the username and password is there in users list
loggedusr.append(user) # if true, add the user to the loggedusr list
found = True
msg = 'Logged in successfully' # display the message that login is success
break
if found == False:
msg = 'Username not available' # if user name and password not found in the users list, the display the message username not available
fr.close() # close the file
return msg
def main():
loadorg() # call the loadorg method to load all the organization record to orglist
try:
ssocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create the server socket
except socket.error: # if error creating socket call the exception
print('socket creation failed')
sys.exit()
hostname = socket.gethostname() # store the local machine name
sport = 5678 # port of the server
ssocket.bind((hostname, sport)) # bind the host name and port
ssocket.listen(5) # setup the socket for multiple connections
csocket, address = ssocket.accept() # set the server socket to receive the connection from client
print('Connected from %s' % (str(address))) # once client connect display client ip address and portnumber
while True:
uname = csocket.recv(4096).decode('ascii') # receive username and password value from client
passwd = csocket.recv(4096).decode('ascii')
status = login(uname, passwd) # call the login method
csocket.send(status.encode('ascii')) # send the status of login back to client
break
while True:
choice = csocket.recv(4096).decode('ascii') # receive the choice of option selected from menu in client
csocket.send(choice.encode('ascii')) # send the choice back to client for acknowledgement
if choice == '1':
organame = csocket.recv(4096).decode('ascii') # receive the organization name from client
status = domainip(organame) # call the domainip method and get the status
if status == 1: # if status if 1 then domain name and IP address is available
global domain
global ip
outdata = domain + ' ' + ip + 'n' # store the domain and ip value in variable
csocket.send(outdata.encode('ascii')) # send the data back to client
else:
csocket.send('Organisation is unknown'.encode('ascii')) # send the organization is unknown to client
continue
elif choice == '2':
minval = calcminimum() # call the calcminimum method to find the minimum value of minutes
maxval = calcmaximum() # call the calcmaximum method to find the maximum value of minutes
meanval = calcmean() # call the calcmean method to find the mean value of minutes
# send the minval, maxval and meanval to client
csocket.send(minval.encode('ascii'))
csocket.send(maxval.encode('ascii'))
csocket.send(meanval.encode('ascii'))
continue
elif choice == '3':
ch = csocket.recv(4096).decode('ascii') # receive the sorting choice from client
outdata = ''
if ch == '1': # if choice is 1, sort the organisation list in ascending order of organization name
orglist.sort(reverse=False, key=getkeyname) # sort the organisation list
for org in orglist:
fields = org.strip().split(' ')
outdata = outdata + fields[0] + ' ' + fields[1] + ' ' + fields[2] + ' ' + fields[3] + 'n'
elif ch == '2': # if choice is 2, sort the organisation list in descending order of minutes
sortlist = []
for org in orglist:
fields = org.strip().split(' ')
outdata = fields[0] + ',' + fields[1] + ',' + fields[2] + ',' + fields[3] + 'n'
sortlist.append(outdata)
outdata = ''
sortlist.sort(reverse=True, key=getkeysize) # sort the list
for org1 in sortlist:
fields = org1.strip().split(',')
outdata = outdata + fields[0] + ' ' + fields[1] + ' ' + fields[2] + ' ' + fields[3] + 'n'
csocket.send(outdata.encode('ascii')) # send the sorted list back to client
continue
elif choice == '4':
# receive data of new organization name, domain name, ip address and minutes from client
oname = csocket.recv(4096).decode('ascii')
dname = csocket.recv(4096).decode('ascii')
iadd = csocket.recv(4096).decode('ascii')
minu = csocket.recv(4096).decode('ascii')
found = False
for org in orglist: #loop through the orglist
if oname in org: # check if new inputted organisation name is there in orglist
found = True
break
if found == False: # if orglist does not have the new organisation name, add the new record to orglist
indata = oname + ' ' + dname + ' ' + iadd + ' ' + minu + 'n'
orglist.append(indata)
fw = open('organisations.txt', 'w') # update the organizations.txt file to record the new organization record
for org in orglist:
fields = org.strip().split(' ')
fw.writelines('%s %s %s %sn' % (fields[0], fields[1], fields[2], fields[3])) # write the record to file
fw.close() # close the file
status = 'True'
else:
status = 'False'
csocket.send(status.encode('ascii')) # send the status of adding record back to client
continue
elif choice == '5':
oname = csocket.recv(4096).decode('ascii') # receive the organizaion name from client
found = False
for org in orglist: # loop through the orglist to check if the given orgnasation name is there in orglist
if oname in org: # if organization name is available, then remove it
orglist.remove(org) # remove the organzation record from orglist
found = True
break
if found == False:
status = 'False'
else:
status = 'True'
fw1 = open('organisations.txt', 'w') # open the organisations.txt file to update after removing the organzation record
for org in orglist:
fields = org.strip().split(' ')
fw1.writelines('%s %s %s %sn' % (fields[0], fields[1], fields[2], fields[3])) #write the record to file
fw1.close()
csocket.send(status.encode('ascii')) # send the status of the removing record to client
continue
elif choice == '6':
print('Good Bye !! Have a nice day!!! ')
break
else:
print('Wrong choice. choose between 1 - 6 only ')
break
ssocket.close() # close the server socket
main() # call the main() function
CLIENT PROGRAM CODING :
import socket # import the socket package
import sys
def main():
csocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create the client socket
hostname = socket.gethostname() # get the host name of local machine
cport = 5678 # set the port where client connects
csocket.connect((hostname,cport)) # connect the client to server
count = 0
while count < 3:
username = input('Enter user name : ') # input user name and password for login
password = input('Enter password : ')
csocket.send(username.encode('ascii')) # send the username and password to server for login
csocket.send(password.encode('ascii'))
msg = csocket.recv(4096).decode('ascii') # get the message from server after login
count = count + 1
if msg == 'Logged in successfully':
break
else:
if count < 3:
print(msg) # is login failed display the error message
continue
else:
sys.exit(0)
print(msg) # display the success login message
while True:
# display the menu options
print()
print('------------------------------')
print()
print(' MENU ')
print()
print('------------------------------')
print()
print('1. Get Domain Name and IP')
print('2. Get Statistics')
print('3. Sort Data')
print('4. Add Organisation')
print('5. Remove Organisation')
print('6. Quit')
choice = input('Enter your choice (1,2,3,4,5, or 6) : ') # get the user choice
csocket.send(choice.encode('ascii')) # send the choice to server for acknowledgement
msg = csocket.recv(4096).decode('ascii') # receive the acknowlegment from server
print('Choice %s selected ' % (str(msg))) # display the message from server
if choice == '1':
orgname = input('Enter organisation name : ') # input the organisation name
csocket.send(orgname.encode('ascii')) # send the organisation name to server
data = csocket.recv(4096).decode('ascii') # receive the data from server
if data != 'Organisation is unknown':
indata = data.strip().split(' ')
# display domain name and ip address
print('Domain Name : %s ' % (indata[0]))
print('IP Address : %s ' % (indata[1]))
else:
print(data) # print the error message received from server
continue
elif choice == '2':
# receive the minimum, maximum and mean value of minutes from server
minvalue = csocket.recv(4096).decode('ascii')
maxvalue = csocket.recv(4096).decode('ascii')
meanvalue = csocket.recv(4096).decode('ascii')
#display the values
print('Minimum number of minutes : %s' % (minvalue))
print('Maximum number of minutes : %s' % (maxvalue))
print('Mean number of minutes : %s' % (meanvalue))
continue
elif choice == '3':
count = 0
while True:
ch = input('1. sort by name, 2 sort by size : ') # input the sorting choice
if ch == '1' or ch == '2' :
csocket.send(ch.encode('ascii')) # send the choice to server to perform the sorting
break
else:
count = count + 1 # if choice is not 1 or 2 ask to enter the choice for 3 times
if count == 3:
sys.exit(0) # terminate after 3 unsuccessful attempts
else:
continue
data = csocket.recv(4096).decode('ascii') # receive the sorted data from server
print(data) # display the sorted data
continue
elif choice == '4':
count = 0
while True:
# input organization name, domain name, ip address and minutes
orgname = input('Enter Organisation name : ')
domname = input('Enter Domain Name : ')
ipadd = input('Enter IP Address : ')
minutes = input('Enter minutes : ')
# send these inputted values to server
csocket.send(orgname.encode('ascii'))
csocket.send(domname.encode('ascii'))
csocket.send(ipadd.encode('ascii'))
csocket.send(minutes.encode('ascii'))
# receive the status of adding record from server
status = csocket.recv(4096).decode('ascii')
count = count + 1
if status == 'True': # if status is true, record saved successfully
print('Organisation data saved successfully')
break
else: # else organization name exists
print('Organisation already exists')
if count <= 3:
continue
else:
sys.exit(0)
break
continue
elif choice == '5':
count = 0
while True:
orgname = input('Enter Organisation Name : ') # input the organisation name for removing
csocket.send(orgname.encode('ascii')) # send the oranisation name to server for removing
status = csocket.recv(4096).decode('ascii') # get the status of removal from server
count = count + 1
if status == 'False': # if status is false, the organisation name does not exist so no removal possible
print('Organisation does not exist')
if count <= 3:
continue
else:
sys.exit(0)
break
else:
print('Organisation record removed successfully')
break
continue
elif choice == '6': # exit the program
print('Good Bye!! Have a nice day')
break
else:
print('Wrong choice. Try again !!!!')
continue
csocket.close() # close the client socket
main()
OUTPUT SCREENS :
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
Enter user name : user8
Enter password : gsd
Logged in successfully
------------------------------
MENU
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 3
Choice 3 selected
-
- sort by name, 2 sort by size : 1
Athens www.athens.org.gr 178.55.12.2 6789
Chandigarh www.cuniv.edu.in 165.205.100.5 1234
Computerpark www.computerpark.com 215.34.100.10 400
Delhi www.jnu.edu.in 115.125.200.5 9876
Istanbul www.istanbul.com.tr 155.44.32.7 2980
London www.london.com.en 186.11.21.1 7893
Mumbai www.iit.edu.in 115.130.100.20 6432
Melbourne www.melbourne.edu.au 199.45.12.3 1245
Newyork www.nyit.com 110.20.100.20 5643
Wipro www.wipro.com 235.125.100.5 6544
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 1
Choice 1 selected
Enter organisation name : Wipro
Domain Name : www.wipro.com
IP Address : 235.125.100.5
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 2
Choice 2 selected
Minimum number of minutes : 400
Maximum number of minutes : 9876
Mean number of minutes : 4903
Get Domain Name and IP
-
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 4
Choice 4 selected
Enter Organisation name : Tata
Enter Domain Name : www.tata.com
Enter IP Address : 100.205.245.200
Enter minutes : 6302
Organisation data saved successfully
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 5
Choice 5 selected
Enter Organisation Name : jhgf
Organisation does not exist
Enter Organisation Name : Tata
Organisation record removed successfully
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 6
Good Bye !! Have a nice day.
Analysis and Design:
The project is a Socket Programming (Network Programming) in python which demonstrate the client-server architecture of the project
In this project there are two components: SERVER and CLIENT.
Server program runs first and it waits for clients to get connected. Once a client get connected it displays the client IP address and port number of client. The server program then authenticate the user name and password which is entered in the client program, by checking the users.txt file which stores the record of all the user. If the user name and password is available, and authentication is successful, it sends the message “Logged in successfully” to the client where this message is displayed. And if the authentication failed then it displays the message “Username not available” if the inputted user name is not available in the user.txt file or “User already logged in” if the user is already to the server. The server keeps a list of all the logged user for this purpose where once the user is logged in successfully, the username of that user is stored in the logged user list.
Pseudocode for login :
-
- Input usrname and paswd in client program
- Send usrname and paswd to server program
- Read “users.txt” file and load all users in userlist
- For usr in userlist
- Check if usrname is present in loggeduser list
- If found set msg = “User already logged in”
- Exit for loop and go to step 5
- Check usrname and paswd present in userlist
- If found set msg = “Logged in successfully”
- Exit for loop and go to step 5
- Else set msg = “Username not available” and exit loop
- Check if usrname is present in loggeduser list
- Send msg to client program
- Display the msg
Server code for login procedure:
Client code for login procedure :
Once the client have logged in successfully, it displays a menu with six options :
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
In option 1, we input the organization name, which is sent to the server. In the server program, it execute a method where it iterate the list of organization records and checks if the organization name is there in the record. If the name exist, it sends back the domain name and IP address of that record back to the client program. If the organization name is not available in the organization list, it send back a message that “Orgnaizartion name is unknown”, which is displayed in the client program
Pseudocode for option 1 :
-
- Input orgname in client program
- Send orgname to server program
- For org in orglist
- Check if orgname in org
- Set outdata = domname + “ “ + ip
- Send outdata to client program
- Check if orgname in org
-
- Exit loop and go to step 4
-
- Else
- Set outdata = “organization is unknown”
- Go to step 1
- Receive outdata from server
- Print outdata
- Else
The server and client part of coding for this option 1 :
Server code :
Client code :
In option 2, the Server program executes methods calcminimum(), calcmaximum() and calcmean() to calculate and find the minimum, maximum and mean value of the time from the organization list and send back these three values to the client program where they are displayed on the screen.
Pseudocode for option 2 :
-
- Call method calcminimum() in server to find the minimum value of minutes
- For org in orglist
- Add minutes field to minminutes list
- Set mindata = minminutes[0]
- For x = 0 to len(minminutes)
- If minminutes[x] < mindata
- Set mindata = minminutes[x]
- Return mindata
- If minminutes[x] < mindata
- Call method calcmaximum in server to find the maximum values of minutes
- For org in orglist
- Add minutes field to maxminutes list
- Set maxdata = maxminutes[0]
- For x = 0 to len(maxminutes)
- If maxminutes[x] > maxdata
- Set maxdata = maxminutes[x]
- Return maxdata
- If maxminutes[x] > maxdata
- Call method calcmean() in server to find the mean value of minutes
- For org in orglist
- Add minutes field to meanminutes list
- Set total = 0
- For x = 0 to len(meanminutes)
- Total = total + meanminutes[x]
- Mean = total / len(meanminutes)
- Return mean
- For org in orglist
- Send mindata, maxdata, mean to client
- Receive mindata, maxdata and mean from server
- Print mindata, maxdata and mean values
- For x = 0 to len(maxminutes)
- For org in orglist
- For x = 0 to len(minminutes)
- For org in orglist
- Call method calcminimum() in server to find the minimum value of minutes
The server and client code for this option 2 :
Server code :
Client Code :
In option 3, the organization list is sorted either on the basis of organization name is ascending order or on the basis of time in minutes in descending order.
Pseudocode for option 3 :
-
- Input sorting order choice in client (1 or 2)
- Send the sort choice to server
- If choice = 1
- Sort orglist in ascending order of organization name
- For org in orglist
- Concatenate the organization data to outdata variable
- Send the outdata to client program
- If choice = 2
- Sort orglist in descending order of minutes
- For org in orglist
- Concatenate the organization data to outdata variable
- Send the outdata to client program
- Receive outdata value from server
- Print outdata
The server and client code for option 3 :
Server Code :
Client code :
In option 4, we are able to store the record of new organization in the organization.txt file. In this option, first we enter organization name, domain name, IP address and time in minutes in the client side. This information is then sent to the server program. In the server program, first we check whether the organization name is already available in the organization list. If the organization name is not there in the organization list, then this new record is added to the organization list which also get updated in the organization.txt file and message is send to the client program “Organization data saved successfully” and if the organization name is available in the organization list, then it displays the message “Organization already exists”. The user is able to enter the new organization record for 3 times and after that the program terminates if the existing organization name is entered 3 times
Pseudocode for option 4 :
-
- Input orgname, domname, ipadr, minu in client
- Send orgname, domname, ipadr, minu to server
- For org in orglist
- Check if orgname in org
- Set status = true
- Exit for loop
- Set status = false
- Exit for loop
- If status = false
- add organization data to orglist
- open “organizations.txt” file for writing
- update the file with new record
- close file
- send status to client
- receive status from server
- if status = true
- print “organization data save successfully
- exit
- else
- print “organization already exist”
- go to step 1
The server and client code for this option 4 :
Server code :
Client code :
In option 5, we are able to remove the record of organization from the organizations.txt file. First we input the name of the organization name in the client part, the organization name is then send to the server program, where it checks whether the organization name is available in the organization list. If name is available it removes the organization record from the list and also update the organizations.txt file by removing the organization record from the file. On the contrary if the organization name is not available in the organization list, the server program send back a message “Organization does not exists” which is displayed in the client program.
Pseudocode for option 5 :
-
- Input organization name, orgname in client program
- Send orgname to server program
- Set status = false
- For org in orglist
- Check if orgname is present in org
- If found, remove org from orglist
- Set status = true
- Check if orgname is present in org
-
- Exit for loop
-
- If status = true
- Open “organizations.txt” file for writing
- Update the file with updated orglist and removing the deleted organization record
- Close file
- Send status to client program
- Receive status from server program
- If status is true
- Print “Organization removed successfully”
- Else if status is false
- Print “Organization does not exists”
- If status = true
The server and client code for option 5 :
Server code :
Program Coding and Output Screens :
SERVER PROGRAM CODING :
import socket # import the socket package to create server and client sockets
import sys
loggedusr = [] # list of the logged user
users = [] #list of all the users
orglist = [] # list of the organization records
domain = ''
ip = ''
q = 0
minminutes = [] # list to minutes to find the minimum, maximum and mean value respectively
maxminutes = []
meanminutes = []
def getkeyname(namelist):
return namelist[0]
def getkeysize(sizelist):
return sizelist[3]
def calcminimum(): # method to find the minimum of the minutes
for org in orglist: # loop the organization list
fields = org.strip().split(' ') # split the record
minminutes.append(int(fields[3])) # store the minutes value in minminutes list
mindata = minminutes[0] # store the minutes of index 0 to mindata
for x in range(0, len(minminutes)): # loop through the minminutes list
if minminutes[x] < mindata: # check if the current minminutes value is < mindata
mindata = minminutes[x] # if true store the current value to mindata
return str(mindata) # return back the mindata
def calcmaximum(): # method to find the maximum of the minutes
for org in orglist: # loop the organization list
fields = org.strip().split(' ') # split the record
maxminutes.append(int(fields[3])) # store the minutes value in maxminutes list
maxdata = maxminutes[0] # store the minutes of index 0 to maxdata
for x in range(0, len(maxminutes)): # loop through the maxminutes list
if maxminutes[x] > maxdata: # check if the current value is > maxdata
maxdata = maxminutes[x] # if true store the current value to maxdata
return str(maxdata) # return back the maxdata
def calcmean(): # method to find the mean value of the minutes
for org in orglist: #loop through the organization list
fields = org.strip().split(' ') # split the record
meanminutes.append(int(fields[3])) # store the minutes value to meanminutes list
total = 0 # initialize the total variable to 0
for x in range(0, len(meanminutes)): # loop through the meanminutes list
total = total + meanminutes[x] # add all the minutes to total variable
avg = int(total / len(meanminutes)) # find the mean value by dividing the total by the length of meanmiutes list
return str(avg) # return back the mean value
def loadorg(): # method to read the organizations.txt file and store the organization record to orglist
fr1 = open('organisations.txt', 'r') # open file for reading
lines = fr1.readlines() # read all the records/lines and store it in lines list
for l in lines: # loop through all the lines
orglist.append(l) # store each organization record to orglist
fr1.close() # close the file
def domainip(orgname): # method to find the domain name and ip address of the given organization name
fields =[]
q = 0
for org in orglist: # loop through the organisation list
if orgname in org: #check if the orgname is there in the current organisation record
fields = org.strip().split(' ') # if true, split the record
global domain
global ip
domain = fields[1] # store the domain value
ip = fields[2] # store the ip address value
q = 1
break
return q
def loggeduser(usrname, pswd): # method to check whether the user is already logged in or not
found = False
for usr in loggedusr: # loop through the logged user list
if usrname + ' ' + pswd in usr: # check if the given username and password is there in current record
found = True # if true set found to true
break
return found # return back the found value
def login(usrname, pswd): # method to login the user with given username and password to the program
fr = open('users.txt', 'r') # open users.txt file for reading
users = fr.readlines() # read all the records of users and store it in users list
found = False
msg = ''
for user in users: # loop through the users list
status = loggeduser(usrname, pswd) # call the loggeduser method to check whether the user is already logged in or not
if status == True: # if status is true, then it means the user is already logged in
found = True
msg = 'User already logged in'
break
else:
if usrname + ' ' + pswd in user: #check if the username and password is there in users list
loggedusr.append(user) # if true, add the user to the loggedusr list
found = True
msg = 'Logged in successfully' # display the message that login is success
break
if found == False:
msg = 'Username not available' # if user name and password not found in the users list, the display the message username not available
fr.close() # close the file
return msg
def main():
loadorg() # call the loadorg method to load all the organization record to orglist
try:
ssocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create the server socket
except socket.error: # if error creating socket call the exception
print('socket creation failed')
sys.exit()
hostname = socket.gethostname() # store the local machine name
sport = 5678 # port of the server
ssocket.bind((hostname, sport)) # bind the host name and port
ssocket.listen(5) # setup the socket for multiple connections
csocket, address = ssocket.accept() # set the server socket to receive the connection from client
print('Connected from %s' % (str(address))) # once client connect display client ip address and portnumber
while True:
uname = csocket.recv(4096).decode('ascii') # receive username and password value from client
passwd = csocket.recv(4096).decode('ascii')
status = login(uname, passwd) # call the login method
csocket.send(status.encode('ascii')) # send the status of login back to client
break
while True:
choice = csocket.recv(4096).decode('ascii') # receive the choice of option selected from menu in client
csocket.send(choice.encode('ascii')) # send the choice back to client for acknowledgement
if choice == '1':
organame = csocket.recv(4096).decode('ascii') # receive the organization name from client
status = domainip(organame) # call the domainip method and get the status
if status == 1: # if status if 1 then domain name and IP address is available
global domain
global ip
outdata = domain + ' ' + ip + 'n' # store the domain and ip value in variable
csocket.send(outdata.encode('ascii')) # send the data back to client
else:
csocket.send('Organisation is unknown'.encode('ascii')) # send the organization is unknown to client
continue
elif choice == '2':
minval = calcminimum() # call the calcminimum method to find the minimum value of minutes
maxval = calcmaximum() # call the calcmaximum method to find the maximum value of minutes
meanval = calcmean() # call the calcmean method to find the mean value of minutes
# send the minval, maxval and meanval to client
csocket.send(minval.encode('ascii'))
csocket.send(maxval.encode('ascii'))
csocket.send(meanval.encode('ascii'))
continue
elif choice == '3':
ch = csocket.recv(4096).decode('ascii') # receive the sorting choice from client
outdata = ''
if ch == '1': # if choice is 1, sort the organisation list in ascending order of organization name
orglist.sort(reverse=False, key=getkeyname) # sort the organisation list
for org in orglist:
fields = org.strip().split(' ')
outdata = outdata + fields[0] + ' ' + fields[1] + ' ' + fields[2] + ' ' + fields[3] + 'n'
elif ch == '2': # if choice is 2, sort the organisation list in descending order of minutes
sortlist = []
for org in orglist:
fields = org.strip().split(' ')
outdata = fields[0] + ',' + fields[1] + ',' + fields[2] + ',' + fields[3] + 'n'
sortlist.append(outdata)
outdata = ''
sortlist.sort(reverse=True, key=getkeysize) # sort the list
for org1 in sortlist:
fields = org1.strip().split(',')
outdata = outdata + fields[0] + ' ' + fields[1] + ' ' + fields[2] + ' ' + fields[3] + 'n'
csocket.send(outdata.encode('ascii')) # send the sorted list back to client
continue
elif choice == '4':
# receive data of new organization name, domain name, ip address and minutes from client
oname = csocket.recv(4096).decode('ascii')
dname = csocket.recv(4096).decode('ascii')
iadd = csocket.recv(4096).decode('ascii')
minu = csocket.recv(4096).decode('ascii')
found = False
for org in orglist: #loop through the orglist
if oname in org: # check if new inputted organisation name is there in orglist
found = True
break
if found == False: # if orglist does not have the new organisation name, add the new record to orglist
indata = oname + ' ' + dname + ' ' + iadd + ' ' + minu + 'n'
orglist.append(indata)
fw = open('organisations.txt', 'w') # update the organizations.txt file to record the new organization record
for org in orglist:
fields = org.strip().split(' ')
fw.writelines('%s %s %s %sn' % (fields[0], fields[1], fields[2], fields[3])) # write the record to file
fw.close() # close the file
status = 'True'
else:
status = 'False'
csocket.send(status.encode('ascii')) # send the status of adding record back to client
continue
elif choice == '5':
oname = csocket.recv(4096).decode('ascii') # receive the organizaion name from client
found = False
for org in orglist: # loop through the orglist to check if the given orgnasation name is there in orglist
if oname in org: # if organization name is available, then remove it
orglist.remove(org) # remove the organzation record from orglist
found = True
break
if found == False:
status = 'False'
else:
status = 'True'
fw1 = open('organisations.txt', 'w') # open the organisations.txt file to update after removing the organzation record
for org in orglist:
fields = org.strip().split(' ')
fw1.writelines('%s %s %s %sn' % (fields[0], fields[1], fields[2], fields[3])) #write the record to file
fw1.close()
csocket.send(status.encode('ascii')) # send the status of the removing record to client
continue
elif choice == '6':
print('Good Bye !! Have a nice day!!! ')
break
else:
print('Wrong choice. choose between 1 - 6 only ')
break
ssocket.close() # close the server socket
main() # call the main() function
import socket # import the socket package
import sys
def main():
csocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create the client socket
hostname = socket.gethostname() # get the host name of local machine
cport = 5678 # set the port where client connects
csocket.connect((hostname,cport)) # connect the client to server
count = 0
while count < 3:
username = input('Enter user name : ') # input user name and password for login
password = input('Enter password : ')
csocket.send(username.encode('ascii')) # send the username and password to server for login
csocket.send(password.encode('ascii'))
msg = csocket.recv(4096).decode('ascii') # get the message from server after login
count = count + 1
if msg == 'Logged in successfully':
break
else:
if count < 3:
print(msg) # is login failed display the error message
continue
else:
sys.exit(0)
print(msg) # display the success login message
while True:
# display the menu options
print()
print('------------------------------')
print()
print(' MENU ')
print()
print('------------------------------')
print()
print('1. Get Domain Name and IP')
print('2. Get Statistics')
print('3. Sort Data')
print('4. Add Organisation')
print('5. Remove Organisation')
print('6. Quit')
choice = input('Enter your choice (1,2,3,4,5, or 6) : ') # get the user choice
csocket.send(choice.encode('ascii')) # send the choice to server for acknowledgement
msg = csocket.recv(4096).decode('ascii') # receive the acknowlegment from server
print('Choice %s selected ' % (str(msg))) # display the message from server
if choice == '1':
orgname = input('Enter organisation name : ') # input the organisation name
csocket.send(orgname.encode('ascii')) # send the organisation name to server
data = csocket.recv(4096).decode('ascii') # receive the data from server
if data != 'Organisation is unknown':
indata = data.strip().split(' ')
# display domain name and ip address
print('Domain Name : %s ' % (indata[0]))
print('IP Address : %s ' % (indata[1]))
else:
print(data) # print the error message received from server
continue
elif choice == '2':
# receive the minimum, maximum and mean value of minutes from server
minvalue = csocket.recv(4096).decode('ascii')
maxvalue = csocket.recv(4096).decode('ascii')
meanvalue = csocket.recv(4096).decode('ascii')
#display the values
print('Minimum number of minutes : %s' % (minvalue))
print('Maximum number of minutes : %s' % (maxvalue))
print('Mean number of minutes : %s' % (meanvalue))
continue
elif choice == '3':
count = 0
while True:
ch = input('1. sort by name, 2 sort by size : ') # input the sorting choice
if ch == '1' or ch == '2' :
csocket.send(ch.encode('ascii')) # send the choice to server to perform the sorting
break
else:
count = count + 1 # if choice is not 1 or 2 ask to enter the choice for 3 times
if count == 3:
sys.exit(0) # terminate after 3 unsuccessful attempts
else:
continue
data = csocket.recv(4096).decode('ascii') # receive the sorted data from server
print(data) # display the sorted data
continue
elif choice == '4':
count = 0
while True:
# input organization name, domain name, ip address and minutes
orgname = input('Enter Organisation name : ')
domname = input('Enter Domain Name : ')
ipadd = input('Enter IP Address : ')
minutes = input('Enter minutes : ')
# send these inputted values to server
csocket.send(orgname.encode('ascii'))
csocket.send(domname.encode('ascii'))
csocket.send(ipadd.encode('ascii'))
csocket.send(minutes.encode('ascii'))
# receive the status of adding record from server
status = csocket.recv(4096).decode('ascii')
count = count + 1
if status == 'True': # if status is true, record saved successfully
print('Organisation data saved successfully')
break
else: # else organization name exists
print('Organisation already exists')
if count <= 3:
continue
else:
sys.exit(0)
break
continue
elif choice == '5':
count = 0
while True:
orgname = input('Enter Organisation Name : ') # input the organisation name for removing
csocket.send(orgname.encode('ascii')) # send the oranisation name to server for removing
status = csocket.recv(4096).decode('ascii') # get the status of removal from server
count = count + 1
if status == 'False': # if status is false, the organisation name does not exist so no removal possible
print('Organisation does not exist')
if count <= 3:
continue
else:
sys.exit(0)
break
else:
print('Organisation record removed successfully')
break
continue
elif choice == '6': # exit the program
print('Good Bye!! Have a nice day')
break
else:
print('Wrong choice. Try again !!!!')
continue
csocket.close() # close the client socket
main()
OUTPUT SCREENS :
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
Enter user name : user8
Enter password : gsd
Logged in successfully
------------------------------
MENU
------------------------------
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 3
Choice 3 selected
-
- sort by name, 2 sort by size : 1
Athens www.athens.org.gr 178.55.12.2 6789
Chandigarh www.cuniv.edu.in 165.205.100.5 1234
Computerpark www.computerpark.com 215.34.100.10 400
Delhi www.jnu.edu.in 115.125.200.5 9876
Istanbul www.istanbul.com.tr 155.44.32.7 2980
London www.london.com.en 186.11.21.1 7893
Mumbai www.iit.edu.in 115.130.100.20 6432
Melbourne www.melbourne.edu.au 199.45.12.3 1245
Newyork www.nyit.com 110.20.100.20 5643
Wipro www.wipro.com 235.125.100.5 6544
------------------------------
MENU
------------------------------
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 1
Choice 1 selected
Enter organisation name : Wipro
Domain Name : www.wipro.com
IP Address : 235.125.100.5
------------------------------
MENU
------------------------------
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 2
Choice 2 selected
Minimum number of minutes : 400
Maximum number of minutes : 9876
Mean number of minutes : 4903
------------------------------
MENU
------------------------------
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 4
Choice 4 selected
Enter Organisation name : Tata
Enter Domain Name : www.tata.com
Enter IP Address : 100.205.245.200
Enter minutes : 6302
Organisation data saved successfully
------------------------------
MENU
------------------------------
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit
Enter your choice (1,2,3,4,5, or 6) : 5
Choice 5 selected
Enter Organisation Name : jhgf
Organisation does not exist
Enter Organisation Name : Tata
Organisation record removed successfully
------------------------------
MENU
------------------------------
-
- Get Domain Name and IP
- Get Statistics
- Sort Data
- Add Organisation
- Remove Organisation
- Quit