Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52b8976

Browse files
committed
Minimal changes to make ftplib work. Basically, this opens the stream in
text mode and encodes commands being sent. The default encoding is ASCII, there's a class/instance variable 'encoding' you could set to change it.
1 parent 93adc5d commit 52b8976

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

Lib/ftplib.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class FTP:
9494
below for details).
9595
The download/upload functions first issue appropriate TYPE
9696
and PORT or PASV commands.
97-
'''
97+
'''
9898

9999
debugging = 0
100100
host = ''
@@ -103,6 +103,7 @@ class FTP:
103103
file = None
104104
welcome = None
105105
passiveserver = 1
106+
encoding = "ASCII"
106107

107108
# Initialization method (called by class instantiation).
108109
# Initialize host to localhost, port to standard ftp port
@@ -128,7 +129,7 @@ def connect(self, host='', port=0, timeout=None):
128129
self.timeout = timeout
129130
self.sock = socket.create_connection((self.host, self.port), self.timeout)
130131
self.af = self.sock.family
131-
self.file = self.sock.makefile('rb')
132+
self.file = self.sock.makefile('r', encoding=self.encoding)
132133
self.welcome = self.getresp()
133134
return self.welcome
134135

@@ -167,7 +168,7 @@ def sanitize(self, s):
167168
def putline(self, line):
168169
line = line + CRLF
169170
if self.debugging > 1: print('*put*', self.sanitize(line))
170-
self.sock.sendall(line)
171+
self.sock.sendall(line.encode(self.encoding))
171172

172173
# Internal: send one command to the server (through putline())
173174
def putcmd(self, line):
@@ -403,7 +404,7 @@ def retrlines(self, cmd, callback = None):
403404
if callback is None: callback = print_line
404405
resp = self.sendcmd('TYPE A')
405406
conn = self.transfercmd(cmd)
406-
fp = conn.makefile('rb')
407+
fp = conn.makefile('r', encoding=self.encoding)
407408
while 1:
408409
line = fp.readline()
409410
if self.debugging > 2: print('*retr*', repr(line))

0 commit comments

Comments
 (0)