Skip to content

Commit d38820c

Browse files
added python files
1 parent a1d8f19 commit d38820c

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

client.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import socket
2+
3+
connip = "192.168.0.101"
4+
connport = 4000
5+
6+
c = socket.socket()
7+
c.connect((connip, connport))
8+
c.settimeout(20)
9+
10+
print("connected")
11+
welcome = c.recv(1024)
12+
print(welcome.decode())
13+
while True:
14+
sender = input("fileshell> ")
15+
#maybe we can put a if here and if its download we can put this in download mode
16+
if(sender == "download"):
17+
choose = input("Name the file you want to download: ")
18+
newname = input("What name do you want to save it as? ")
19+
c.send(sender.encode())
20+
c.send(choose.encode())
21+
#test = c.recv(1024).decode()
22+
#if(test == "no file"):
23+
# print("Error: the file does not exist")
24+
# continue
25+
newfile = open(newname, "wb")
26+
print("File exists! Downloading file...")
27+
content = c.recv(1024)
28+
while(content):
29+
try:
30+
newfile.write(content)
31+
content = c.recv(1024)
32+
except socket.timeout:
33+
newfile.close()
34+
print("Done downloading")
35+
break
36+
continue
37+
except ValueError:
38+
break
39+
continue
40+
newfile.close()
41+
print("Done downloading")
42+
continue
43+
if(sender == "list"):
44+
sender = sender.encode()
45+
c.send(sender)
46+
warnnote = c.recv(1024)
47+
print(warnnote.decode())
48+
print(" ")
49+
filelist = c.recv(4096)
50+
filelist = filelist.decode()
51+
filelist = eval(filelist)
52+
for file in filelist:
53+
print(file)
54+
continue
55+
sender = sender.encode()
56+
c.send(sender)
57+
textback = c.recv(1024)
58+
textback = textback.decode()
59+
print(textback)
60+
61+

server.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import socket
2+
import os
3+
import os.path
4+
5+
serverpath = "/root/serv"
6+
7+
myip = "192.168.0.101"
8+
myport = 4000
9+
10+
s = socket.socket()
11+
s.bind((myip, myport))
12+
s.listen(5)
13+
sock, addr = s.accept()
14+
print(addr)
15+
wel = f"Welcome to {socket.gethostname()}"
16+
sock.send(wel.encode())
17+
while True:
18+
filecommand = sock.recv(1024)
19+
if(filecommand.decode() == "help"):
20+
back = "commands:\nhelp - list this\nlist - list directory\ndownload - lets you download a file\ncloseserver - close socket"
21+
sock.send(back.encode())
22+
elif(filecommand.decode() == "list"):
23+
note = "Note: this only lists files, dirs are not supported"
24+
sock.send(note.encode())
25+
filelist = [f for f in os.listdir(serverpath) if os.path.isfile(os.path.join(serverpath,f))]
26+
filelist = str(filelist)
27+
sock.send(filelist.encode())
28+
elif(filecommand.decode() == "download"):
29+
whatfile = sock.recv(1024).decode()
30+
if(os.path.exists(serverpath + "/" + whatfile)):
31+
#goodnote = "exists"
32+
#goodcheck = sock.send(goodnote.encode())
33+
openfile = serverpath + "/" + whatfile
34+
sendfile = open(openfile, "rb")
35+
readcontent = sendfile.read(1024)
36+
while(readcontent):
37+
sock.send(readcontent)
38+
readcontent = sendfile.read(1024)
39+
sendfile.close()
40+
else:
41+
cool = "no file"
42+
sock.send(cool.encode())
43+
elif(filecommand.decode() == "closeserver"):
44+
s.close()
45+
quit()
46+
else:
47+
back = "invalid command - use help for help"
48+
sock.send(back.encode())

0 commit comments

Comments
 (0)