Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a1ea2b8

Browse files
Add files via upload
1 parent c8bde36 commit a1ea2b8

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

‎client.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import socket
2+
3+
IP = socket.gethostbyname(socket.gethostname())
4+
PORT = 4456
5+
ADDR = (IP, PORT)
6+
FORMAT = "utf-8"
7+
SIZE = 1024
8+
9+
def main():
10+
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
11+
client.connect(ADDR)
12+
13+
while True:
14+
data = client.recv(SIZE).decode(FORMAT)
15+
cmd, msg = data.split("@")
16+
17+
if cmd == "DISCONNECTED":
18+
print(f"[SERVER]: {msg}")
19+
break
20+
elif cmd == "OK":
21+
print(f"{msg}")
22+
23+
data = input("> ")
24+
data = data.split(" ")
25+
cmd = data[0]
26+
27+
if cmd == "HELP":
28+
client.send(cmd.encode(FORMAT))
29+
elif cmd == "LOGOUT":
30+
client.send(cmd.encode(FORMAT))
31+
break
32+
elif cmd == "LIST":
33+
client.send(cmd.encode(FORMAT))
34+
elif cmd == "DELETE":
35+
client.send(f"{cmd}@{data[1]}".encode(FORMAT))
36+
elif cmd == "UPLOAD":
37+
path = data[1]
38+
39+
with open(f"{path}", "r") as f:
40+
text = f.read()
41+
42+
filename = path.split("/")[-1]
43+
send_data = f"{cmd}@{filename}@{text}"
44+
client.send(send_data.encode(FORMAT))
45+
46+
print("Disconnected from the server.")
47+
client.close()
48+
49+
if __name__ == "__main__":
50+
main()

‎client_data/data.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A simple text file that need to send across the network to the server.
2+
Thank you.

‎client_data/yt.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Idiot Developer - youtube.com/idiotdeveloper.
2+
Welcome you all.

‎server.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
import os
3+
import socket
4+
import threading
5+
6+
IP = socket.gethostbyname(socket.gethostname())
7+
PORT = 4456
8+
ADDR = (IP, PORT)
9+
SIZE = 1024
10+
FORMAT = "utf-8"
11+
SERVER_DATA_PATH = "server_data"
12+
13+
def handle_client(conn, addr):
14+
print(f"[NEW CONNECTION] {addr} connected.")
15+
conn.send("OK@Welcome to the File Server.".encode(FORMAT))
16+
17+
while True:
18+
data = conn.recv(SIZE).decode(FORMAT)
19+
data = data.split("@")
20+
cmd = data[0]
21+
22+
if cmd == "LIST":
23+
files = os.listdir(SERVER_DATA_PATH)
24+
send_data = "OK@"
25+
26+
if len(files) == 0:
27+
send_data += "The server directory is empty"
28+
else:
29+
send_data += "\n".join(f for f in files)
30+
conn.send(send_data.encode(FORMAT))
31+
32+
elif cmd == "UPLOAD":
33+
name, text = data[1], data[2]
34+
filepath = os.path.join(SERVER_DATA_PATH, name)
35+
with open(filepath, "w") as f:
36+
f.write(text)
37+
38+
send_data = "OK@File uploaded successfully."
39+
conn.send(send_data.encode(FORMAT))
40+
41+
elif cmd == "DELETE":
42+
files = os.listdir(SERVER_DATA_PATH)
43+
send_data = "OK@"
44+
filename = data[1]
45+
46+
if len(files) == 0:
47+
send_data += "The server directory is empty"
48+
else:
49+
if filename in files:
50+
os.system(f"rm {SERVER_DATA_PATH}/{filename}")
51+
send_data += "File deleted successfully."
52+
else:
53+
send_data += "File not found."
54+
55+
conn.send(send_data.encode(FORMAT))
56+
57+
elif cmd == "LOGOUT":
58+
break
59+
elif cmd == "HELP":
60+
data = "OK@"
61+
data += "LIST: List all the files from the server.\n"
62+
data += "UPLOAD <path>: Upload a file to the server.\n"
63+
data += "DELETE <filename>: Delete a file from the server.\n"
64+
data += "LOGOUT: Disconnect from the server.\n"
65+
data += "HELP: List all the commands."
66+
67+
conn.send(data.encode(FORMAT))
68+
69+
print(f"[DISCONNECTED] {addr} disconnected")
70+
conn.close()
71+
72+
def main():
73+
print("[STARTING] Server is starting")
74+
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
75+
server.bind(ADDR)
76+
server.listen()
77+
print(f"[LISTENING] Server is listening on {IP}:{PORT}.")
78+
79+
while True:
80+
conn, addr = server.accept()
81+
thread = threading.Thread(target=handle_client, args=(conn, addr))
82+
thread.start()
83+
print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")
84+
85+
if __name__ == "__main__":
86+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /