server.py
1: #!/usr/bin/python
2: import socket
3: s = socket.socket()
4: print 'Server Name : ', #socket.gethostbyaddr('127.0.0.1')
5: s.bind(('127.0.0.1', 1234))
6: s.listen(5)
7: c, addr = s.accept()
8: print 'Got connection from', addr
9: c.send("Thank you for connecting.........")
10: fname = c.recv(4096) # recieving file name from client
11: while(1):
12: fd = open(fname, "r+") # open file for reading
13: str1 = fd.read();
14: fd.close()
15: c.sendall(str1) # send contents of file
16: choice = c.recv(1024)
17: if choice == '1':
18: print "Successfully sent file !!!!!\n";
19: elif choice == '2':
20: print "Successfully sent file !!!!!\n"
21: elif choice == '3':
22: str2 = c.recv(99999)
23: fo = open(fname, "w+")
24: fo.write(str2);
25: fo.close()
26: print "successfully modified file at server side !!!!!\n"
27: elif choice == '4':
28: exit(0);
29: c.close()
30: client.py
31: #!/usr/bin/python
32: import socket # Import socket module
33: s = socket.socket()
34: s.connect(('127.0.0.1', 1234))
35: print s.recv(1024)
36: print "\nEnter file name to be received from server: " # name of file to be received from server
37: fname = raw_input()
38: s.sendall(fname)
39: while(1):
40: str1 = s.recv(99999) # recieve contents of file
41: print "\n##################### What operation do you want to perform #####################\n"
42: print "1.Display the contents of received file on console"
43: print "2.Create a new file with received contents"
44: print "3.Create a new file with received contents along with modification"
45: print "4.Exit\n"
46: print "Enter your choice: "
47: choice = raw_input()
48: s.sendall(choice)
49: if choice == '1':
50: print "\nContents of received file: "
51: print str1, "\n"
52: elif choice == '2':
53: print "\nEnter name of file to be created: "
54: fname = raw_input()
55: fd = open(fname, "w+")
56: fd.write(str1);
57: fd.close()
58: print "\nSuccessfully created file with received data !!!!!"
59: elif choice == '3':
60: print "\nEnter name of file to be created: "
61: fname = raw_input()
62: fd = open(fname, "w+")
63: fd.write(str1);
64: print "\nEnter more data for adding to file: "
65: fdata = raw_input()
66: fd.write(fdata)
67: fd.close()
68: fd = open(fname, 'r+')
69: str2 = fd.read()
70: fd.close()
71: s.sendall(str2)
72: print "\nSuccessfully created file with modified data !!!!!"
73: elif choice == '4':
74: exit(0);
75: else:
76: print "\nYou have entered wrong choice !!!!!"
77: s.close()
Tags:
PROGRAMMING