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