python,Pyqt5 实现FTP服务器与客户端文件上传,下载
时间: 2024-04-08 18:29:52 浏览: 187
可以使用Python和PyQt5来实现FTP服务器和客户端文件上传和下载功能。首先,你需要使用Python内置的ftplib模块来处理FTP连接和操作。下面是一个简单的示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from PyQt5.QtCore import Qt
from ftplib import FTP
class FTPClient(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("FTP Client")
self.setGeometry(100, 100, 300, 200)
self.host = "ftp.example.com" # FTP服务器地址
self.user = "username" # FTP登录用户名
self.password = "password" # FTP登录密码
self.ftp = FTP()
self.connectFTP()
def connectFTP(self):
try:
self.ftp.connect(self.host)
self.ftp.login(self.user, self.password)
self.statusBar().showMessage("Connected to FTP server")
except Exception as e:
QMessageBox.critical(self, "Error", str(e))
sys.exit()
def uploadFile(self, filename):
try:
with open(filename, "rb") as file:
self.ftp.storbinary("STOR " + filename, file)
self.statusBar().showMessage("File uploaded successfully")
except Exception as e:
QMessageBox.critical(self, "Error", str(e))
def downloadFile(self, filename):
try:
with open(filename, "wb") as file:
self.ftp.retrbinary("RETR " + filename, file.write)
self.statusBar().showMessage("File downloaded successfully")
except Exception as e
阅读全文