Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions FTP Server/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import socket
IP=socket.gethostbyname(socket.gethostname())
PORT=3030
FORMAT='utf-8'
HEADER=10
DISCONNECT_MESSAGE="disconnect"

ADDR=(IP,PORT)

client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

client.connect(ADDR)
print(client.recv(1024).decode(FORMAT))
def send(msg):
message=msg.encode(FORMAT)
client.send(message)

def RETR(client,path):
data = client.recv(1024).decode()

with open(path,'w') as f:
data = client.recv(1024).decode()
f.write(data)

def STOR(client,path):
with open(path,'rb') as f:
data = f.read()
client.sendall(data)

def authorization():

user=input("Enter username:")
send(user)
passw=input("Enter password:")
send(passw)
s=client.recv(1024).decode(FORMAT)
if s=="11":
print("Logged in as admin")
return 1
elif s=="10":
print("Login successful")
return 0

elif s=="3":
print("You are a new user do you want to sign up.\nIf yes press 1,else 0")
ch=int(input("1/0:"))
if ch==1:
send("1")
print("Registered as a new user")
else :
send("hfjd")
print("Thank you")
client.close()

return 0
elif s=="4":
print("You are banned from the server")
client.close()

else :
print("Invalid credentials ")
client.close()


def LIST(client):
print(client.recv(1024).decode(FORMAT))


def RETR(client,path):
data = client.recv(1024).decode()

with open(path,'w') as f:
data = client.recv(1024).decode()
f.write(data)

print("File retrieved succesfully!")




role=authorization()

if role==0:
while True:
command = input("Enter a command (LIST, RETR <filename>, STOR <filename>or QUIT): ").strip()
send(command)
if command == "LIST":
LIST(client)
elif command.startswith("RETR"):
_, filename = command.split(' ', 1)

RETR(client,filename.strip())
elif command.startswith("STOR"):
_, filename = command.split(' ', 1)

STOR(client,filename)
elif command == "QUIT":
print("Disconnecting from the server...")
break
else:
print("Invalid command. Please try again.")
if role == 1:
while True:
command = input("Enter a command (ADDUSER <username> <password>, DELUSER <username>, BAN <username>,UNBAN <username>,QUIT): ").strip()
send(command)
if command.startswith("ADDUSER"):
_, username, password = command.split(' ', 2)
print(f"{username} is successfully added to the server")


elif command.startswith("DELUSER"):
_, username = command.split(' ', 1)
print(f"{username} is deleted from the server")

elif command.startswith("BAN"):
_, username = command.split(' ', 1)
print(f"{username} is banned from the server")

elif command.startswith("UNBAN"):
_, username = command.split(' ', 1)
print(f"{username} is unbanned from the server")


elif command == "QUIT":
print("Disconnecting from the server...")
break
else:
print("Invalid command. Please try again.")


client.close()
163 changes: 163 additions & 0 deletions FTP Server/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import socket
import threading
import time
import os

IP = socket.gethostbyname(socket.gethostname())
PORT = 3030
FORMAT = 'utf-8'
HEADER = 10
DISCONNECT_MESSAGE = "disconnect"

ADDR = (IP, PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

def handle_client(conn, addr, user, pass_user,ban,unban):
print(f"[New connection]: {addr} connected")
connected = True
conn.send("You are successfully connected to the server. Please respond by typing the asked details".encode(FORMAT))

while connected:
out = auth(conn, user, pass_user,ban)
if out == 0:
break

if out==1:
while True:
try:
command = conn.recv(1024).decode(FORMAT)
if command == "LIST":
ls(conn)
elif command.startswith("RETR"):
_, filename = command.split(' ', 1)

retr(conn,filename.strip())
elif command.startswith("STOR"):
_, filename = command.split(' ', 1)
store(conn,filename)
elif command == "QUIT":
print(f"Client {addr} disconnected")
conn.close()
connected = False
break
else:
print("Unknown command received.")
break

except Exception as e:
print(f"Error occurred: {e}")
break
if out==2:
while True:
try:
command = conn.recv(1024).decode(FORMAT)

if command.startswith("ADDUSER"):
_, username, password = command.split(' ', 2)
adduser(username,password,user,pass_user)
elif command.startswith("DELUSER"):
_, username = command.split(' ', 1)
delete(username,user,pass_user)
elif command.startswith("BAN"):
_, username = command.split(' ', 1)
ban(username,ban)
elif command.startswith("UNBAN"):
_, username = command.split(' ', 1)
unban(username,ban)
elif command == "QUIT":
print(f"Client {addr} disconnected")
conn.close()
connected = False
break
else:
print("Unknown command received.")
except Exception as e:
print(f"Error occurred: {e}")
break



def auth(conn, user, pass_user,ban):
username = conn.recv(1024).decode(FORMAT)
passw = conn.recv(1024).decode(FORMAT)
if (username in user) and (username not in ban):

if passw == pass_user[user.index(username)]:
if passw=="letmesleep" and username=="admin":
conn.send("11".encode(FORMAT))
return 2
else:
conn.send("10".encode(FORMAT))
return 1
else:
conn.send("Invalid credentials".encode(FORMAT))
conn.close()
return 0
elif ( username not in ban):
conn.send("3".encode(FORMAT))
res = conn.recv(1024).decode(FORMAT)
if res == "1":
user.append(username)
pass_user.append(passw)
print(user)
print(pass_user)
return 1
else:
conn.close()
return 0
else:
conn.send("4".encode(FORMAT))

def ls(client):
dirs = os.listdir("./")
client.send("\n".join(dirs).encode())

def retr(client,path):
client.send(path.encode())
time.sleep(0.1)
with open(path,'rb') as f:
data = f.read()
client.sendall(data)

def store(conn,path):
stream = conn.recv(1024).decode()

with open(str(path),'w') as f:
f.write(stream)

conn.send(b"File stored succesfully!")
def ban(username,ban):
ban.append(username)

def unban(username,ban):
ban.remove(username)

def delete(username,user,pass_user):
i=user.index(username)
user.remove(username)
pass_user.pop(i)


def adduser(username,password,user,pass_user):
user.append(username)
pass_user.append(password)



def start():
server.listen()
print("Server is listening")
user = ['admin']
pass_user = ['letmesleep']
ban=[]
unban=[]

while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr, user, pass_user,ban,unban))
thread.start()
print(f'[Active clients]: ({threading.active_count() - 1})')

print("SERVER is starting")
start()
1 change: 1 addition & 0 deletions OOPS/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.out
Loading