mirror of
https://github.com/Steffo99/fermi-file-server.git
synced 2024-11-24 00:34:18 +00:00
First commit
This commit is contained in:
commit
af9ee7afd1
4 changed files with 182 additions and 0 deletions
0
.gitignore
vendored
Normal file
0
.gitignore
vendored
Normal file
94
cute_client.py
Normal file
94
cute_client.py
Normal file
|
@ -0,0 +1,94 @@
|
|||
from PyQt5 import Qt
|
||||
import os
|
||||
import pickle
|
||||
import socket
|
||||
|
||||
app = Qt.QApplication([])
|
||||
|
||||
class MainWindow(Qt.QMainWindow):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.setWindowTitle("Cute Fileserver Client")
|
||||
# Create a container object
|
||||
self._container = Qt.QWidget()
|
||||
self.setCentralWidget(self._container)
|
||||
# Create a grid
|
||||
self.grid = Qt.QGridLayout()
|
||||
# Create the ip address selection text field
|
||||
self.address_field = Qt.QLineEdit()
|
||||
self.address_field.setPlaceholderText("127.0.0.1:3001")
|
||||
self.grid.addWidget(self.address_field)
|
||||
# Create the connect button
|
||||
self.connect_btn = Qt.QPushButton()
|
||||
self.connect_btn.setText("Connect")
|
||||
self.connect_btn.clicked.connect(self.connect)
|
||||
self.grid.addWidget(self.connect_btn)
|
||||
# Create the file selection combobox
|
||||
self.file_selector = Qt.QComboBox()
|
||||
self.file_selector.setEnabled(False)
|
||||
self.grid.addWidget(self.file_selector)
|
||||
# Create the download button
|
||||
self.download_btn = Qt.QPushButton()
|
||||
self.download_btn.setText("Download")
|
||||
self.download_btn.setEnabled(False)
|
||||
self.download_btn.clicked.connect(self.download)
|
||||
self.grid.addWidget(self.download_btn)
|
||||
# Set the grid as layout
|
||||
self._container.setLayout(self.grid)
|
||||
# Extra data
|
||||
self.client_socket = None
|
||||
|
||||
def connect(self):
|
||||
# Disable the connect button
|
||||
self.address_field.setEnabled(False)
|
||||
self.connect_btn.setEnabled(False)
|
||||
# Split address and port
|
||||
try:
|
||||
address, ports = self.address_field.text().split(":", 1)
|
||||
except IndexError:
|
||||
address = self.address_field.text()
|
||||
ports = "3001"
|
||||
try:
|
||||
port = int(ports)
|
||||
except ValueError:
|
||||
print("Invalid port number")
|
||||
return
|
||||
# Connect to the server
|
||||
self.client_socket = socket.socket()
|
||||
self.client_socket.connect((address, port))
|
||||
# Get the file list
|
||||
files = pickle.loads(self.client_socket.recv(4096))
|
||||
separator = {
|
||||
"posix": "/", # Linux
|
||||
"nt": "\\", # Windows
|
||||
"java": "/" # MacOS
|
||||
}
|
||||
for directory in files:
|
||||
for file in directory[2]:
|
||||
self.file_selector.addItem(f"{directory[0]}{separator[os.name]}{file}")
|
||||
# Toggle the enabled status on the download buttons
|
||||
self.file_selector.setEnabled(True)
|
||||
self.download_btn.setEnabled(True)
|
||||
|
||||
def download(self):
|
||||
# Disable the download buttons
|
||||
self.file_selector.setEnabled(False)
|
||||
self.download_btn.setEnabled(False)
|
||||
# Send the requested filename
|
||||
self.client_socket.send(bytes(self.file_selector.currentText(), encoding="utf8"))
|
||||
# Receive the status from the server
|
||||
status = int(str(self.client_socket.recv(256), encoding="utf8"))
|
||||
# Create an empty file
|
||||
with open("download/" + self.file_selector.currentText(), "w") as file:
|
||||
file.write("")
|
||||
# Download the file
|
||||
while status > 0:
|
||||
data = self.client_socket.recv(256)
|
||||
with open("download/" + self.file_selector.currentText(), "ab") as file:
|
||||
file.write(data)
|
||||
status -= 256
|
||||
|
||||
mw = MainWindow()
|
||||
mw.show()
|
||||
|
||||
app.exec_()
|
51
friendly_client.py
Normal file
51
friendly_client.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
import socket
|
||||
import sys
|
||||
import time
|
||||
import pickle
|
||||
import os
|
||||
|
||||
c = socket.socket()
|
||||
|
||||
address = sys.argv[1]
|
||||
port = sys.argv[2]
|
||||
filename = sys.argv[3] if len(sys.argv) > 3 else None
|
||||
|
||||
c.connect((address, int(port)))
|
||||
files = pickle.loads(c.recv(4096))
|
||||
if filename is None:
|
||||
separator = {
|
||||
"posix": "/", # Linux
|
||||
"nt": "\\", # Windows
|
||||
"java": "/" # MacOS
|
||||
}
|
||||
for directory in files:
|
||||
for file in directory[2]:
|
||||
print(f"{directory[0]}{separator[os.name]}{file}")
|
||||
c.close()
|
||||
sys.exit(2)
|
||||
|
||||
c.send(bytes(filename, encoding="utf8"))
|
||||
|
||||
status = str(c.recv(256), encoding="utf8")
|
||||
if status == "NO\n":
|
||||
print("No such file exists on the server.")
|
||||
else:
|
||||
status = int(status)
|
||||
print("Download started.")
|
||||
|
||||
with open("download/" + filename, "w") as file:
|
||||
file.write("")
|
||||
|
||||
ts = time.time()
|
||||
size = status
|
||||
while status > 0:
|
||||
data = c.recv(256)
|
||||
with open("download/" + filename, "ab") as file:
|
||||
file.write(data)
|
||||
print("█", end="")
|
||||
status -= 256
|
||||
download_time = time.time() - ts
|
||||
print(f"\nDownload complete."
|
||||
f"Download time: {download_time}"
|
||||
f"Download size: {size}"
|
||||
f"Download speed: {size / download_time}")
|
37
server.py
Normal file
37
server.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
import socketserver
|
||||
import os
|
||||
import socket
|
||||
import pickle
|
||||
|
||||
class FileServerRequestHandler(socketserver.BaseRequestHandler):
|
||||
def handle(self):
|
||||
files = list(os.walk("."))
|
||||
self.request.send(pickle.dumps(files))
|
||||
filename = str(self.request.recv(1024), encoding="utf8").strip("\n")
|
||||
if filename == "":
|
||||
self.request.close()
|
||||
return
|
||||
file_path = os.path.relpath(filename)
|
||||
if os.path.isfile(file_path):
|
||||
print(f"Serving {file_path}")
|
||||
with open(file_path, "rb") as file:
|
||||
file.seek(0, 2)
|
||||
file_size = file.tell()
|
||||
file.seek(0, 0)
|
||||
self.request.send(bytes(str(file_size), encoding="utf8") + b"\n")
|
||||
while file_size > 0:
|
||||
self.request.send(file.read(256))
|
||||
file_size -= 256
|
||||
self.request.shutdown(socket.SHUT_RDWR)
|
||||
self.request.close()
|
||||
else:
|
||||
self.request.send(b"NO\n")
|
||||
self.request.shutdown(socket.SHUT_RDWR)
|
||||
self.request.close()
|
||||
|
||||
|
||||
with socketserver.ThreadingTCPServer(("0.0.0.0", 3001), FileServerRequestHandler) as ss:
|
||||
try:
|
||||
ss.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
Loading…
Reference in a new issue