我想使用Python的ftplib客户端指定端口(而不是默认端口21)。
代码如下:
from ftplib import FTP
ftp = FTP('localhost') # connect to host, default port有没有一种简单的方法来指定替代端口?
发布于 2013-06-20 10:19:13
>>> from ftplib import FTP
>>> HOST = "localhost"
>>> PORT = 12345 # Set your desired port number
>>> ftp = FTP()
>>> ftp.connect(HOST, PORT)发布于 2017-09-16 02:06:35
在搜索了许多解决方案之后,docs.python.org和connect命令的组合解决了我的问题。
from ftplib import FTP_TLS
host = 'host'
port = 12345
usr = 'user'
pwd = 'password'
ftps = FTP_TLS()
ftps.connect(host, port)
# Output: '220 Server ready for new user.'
ftps.login(usr, pwd)
# Output: '230 User usr logged in.'
ftps.prot_p()
# Output: '200 PROT command successful.'
ftp.nlst()
# Output: ['mysubdirectory', 'mydoc']如果你使用的是普通FTP而不是FTPS,那么只需使用ftplib.FTP即可。
发布于 2013-06-20 10:19:39
是的,您可以使用connect
from ftplib import FTP
my_ftp = FTP()
my_ftp.connect('localhost', 80) # 80 is the port for examplehttps://stackoverflow.com/questions/17204276
复制相似问题