File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ # server
2+
3+ import socket # Import socket module
4+
5+ port = 60000 # Reserve a port for your service.
6+ s = socket .socket () # Create a socket object
7+ host = socket .gethostname () # Get local machine name
8+ s .bind ((host , port )) # Bind to the port
9+ s .listen (5 ) # Now wait for client connection.
10+
11+ print 'Server listening....'
12+
13+ while True :
14+ conn , addr = s .accept () # Establish connection with client.
15+ print 'Got connection from' , addr
16+ data = conn .recv (1024 )
17+ print ('Server received' , repr (data ))
18+
19+ filename = 'mytext.txt'
20+ f = open (filename ,'rb' )
21+ l = f .read (1024 )
22+ while (l ):
23+ conn .send (l )
24+ print ('Sent ' ,repr (l ))
25+ l = f .read (1024 )
26+ f .close ()
27+
28+ print ('Done sending' )
29+ conn .send ('Thank you for connecting' )
30+ conn .close ()
31+
32+
33+ # client side server
34+
35+ import socket # Import socket module
36+
37+ s = socket .socket () # Create a socket object
38+ host = socket .gethostname () # Get local machine name
39+ port = 60000 # Reserve a port for your service.
40+
41+ s .connect ((host , port ))
42+ s .send ("Hello server!" )
43+
44+ with open ('received_file' , 'wb' ) as f :
45+ print 'file opened'
46+ while True :
47+ print ('receiving data...' )
48+ data = s .recv (1024 )
49+ print ('data=%s' , (data ))
50+ if not data :
51+ break
52+ # write data to a file
53+ f .write (data )
54+
55+ f .close ()
56+ print ('Successfully get the file' )
57+ s .close ()
58+ print ('connection closed' )
You can’t perform that action at this time.
0 commit comments