Skip to content

Commit bb458f6

Browse files
committed
if __name == '__main__': to allow doctests
1 parent dc1de94 commit bb458f6

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

file_transfer_protocol/ftp_client_server.py

+49-50
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,55 @@
22

33
import socket # Import socket module
44

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.
3111

32-
# client side server
33-
34-
import socket # Import socket module
12+
print('Server listening....')
3513

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')
4514
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')

file_transfer_protocol/ftp_send_receive.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
"""
1010

1111
from ftplib import FTP
12-
ftp = FTP('xxx.xxx.x.x') # Enter the ip address or the domain name here
13-
ftp.login(user='username', passwd='password')
14-
ftp.cwd('/Enter the directory here/')
12+
13+
14+
if __name__ == '__main__':
15+
ftp = FTP('xxx.xxx.x.x') # Enter the ip address or the domain name here
16+
ftp.login(user='username', passwd='password')
17+
ftp.cwd('/Enter the directory here/')
1518

1619
"""
1720
The file which will be received via the FTP server

0 commit comments

Comments
 (0)