Python提供了多种方法来从服务器读取路径文件/文件夹。以下是一些常用的方法:
import urllib.request
url = 'http://example.com/path/to/file.txt'
response = urllib.request.urlopen(url)
data = response.read()
print(data)
import requests
url = 'http://example.com/path/to/file.txt'
response = requests.get(url)
data = response.text
print(data)
from ftplib import FTP
ftp = FTP('ftp.example.com')
ftp.login(user='username', passwd='password')
ftp.cwd('/path/to')
filename = 'file.txt'
with open(filename, 'wb') as f:
ftp.retrbinary('RETR ' + filename, f.write)
ftp.quit()
import paramiko
hostname = 'example.com'
port = 22
username = 'username'
password = 'password'
remote_path = '/path/to/file.txt'
local_path = 'file.txt'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, port, username, password)
sftp = ssh.open_sftp()
sftp.get(remote_path, local_path)
sftp.close()
ssh.close()
这些方法可以根据具体的需求选择使用。
领取专属 10元无门槛券
手把手带您无忧上云