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
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"iostream": "cpp",
"ostream": "cpp"
}
}
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
178 changes: 178 additions & 0 deletions FTP Server/FTP_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import socket
import os
import threading

def receive_list(client):

files_list = client.recv(1024).decode()
files = files_list.split("\n")
return files

def send_file(client, filename):
with open(filename, 'rb') as file:
file_content = file.read()
client.send(file_content)
print("File Stored Successfully")

def RETR_command(client, filename):
file_content = client.recv(1024)
with open(filename, 'wb') as file:
file.write(file_content)
print("File retrieved successfully!")

def user():
username = input("Enter your username: ")
password = input("Enter your password: ")

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IP_ADDR = socket.gethostbyname(socket.gethostname())
PORT = 5050

client.connect((IP_ADDR, PORT))

while True:
message = client.recv(1024).decode()

if message == "User?":
client.send(username.encode())

elif message == "Pass?":
client.send(password.encode())

elif message == "User valid":
print("Authentication Successful!")
print("1. LIST: This command should list all the files in the current directory.\n2. RETR <filename>: This command should be used to retrieve a file from the server.\n3. STOR <filename>: This command should be used to store a file on the server.\n4. QUIT: This command should be used to disconnect from the server.")

elif message == "Command?":
command = input("\nEnter valid command: ")
client.send(command.encode())
commandtype, *args = command.split()

if commandtype == "LIST":
files = receive_list(client)
print("\n**List of files on the server**:")
for file in files:
print(file)
continue

elif commandtype == "RETR":
filename = args[0]
RETR_command(client, filename)
continue

elif commandtype == "STOR":
continue

elif commandtype == "QUIT":
print("[DISCONNECTING] disconnecting from server...")
client.close()

else:
continue

elif message == "OK":
filename = args[0]
send_file(client, filename)
continue

elif message == "File not found":
print("No such file was found. Check again")
continue

elif message == "FileExists":
print("File Already Exists!")
continue

elif message == "Invalid command!":
print("ENTER A VALID COMMAND!")
continue

elif message == "User Invalid":
print("Authentication Unsuccessful!\nTRY AGAIN!")
client.close()

def admin():
username = input("Enter your username: ")
password = input("Enter your password: ")

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IP_ADDR = socket.gethostbyname(socket.gethostname())
PORT = 5050

client.connect((IP_ADDR, PORT))

while True:
message = client.recv(1024).decode()

if message == "User?":
client.send(username.encode())

elif message == "Pass?":
client.send(password.encode())

elif message == "User valid":
print("Authentication Successful!")
print("1. LIST: This command should list all the files in the current directory.\n2. RETR <filename>: This command should be used to retrieve a file from the server.\n3. STOR <filename>: This command should be used to store a file on the server.\n4. QUIT: This command should be used to disconnect from the server.\n5. ADDUSER <username> <password>: This command should be used to add a new user to the server.\n6. DELUSER <username>: This command should be used to delete a user from the server.")

elif message == "Command?":
command = input("\nEnter valid command: ")
client.send(command.encode())
commandtype, *args = command.split()

if commandtype == "LIST":
files = receive_list(client)
print("\n**List of files on the server**:")
for file in files:
print(file)
continue

elif commandtype == "RETR":
filename = args[0]
RETR_command(client, filename)
continue

elif commandtype == "STOR":
continue

elif commandtype == "QUIT":
print("[DISCONNECTING] disconnecting from server...")
client.close()

elif commandtype == "ADDUSER":
print("New User Added Successfully")
continue

elif commandtype == "DELUSER":
print("DELETED USER")

elif message == "OK":
filename = args[0]
send_file(client, filename)
continue

elif message == "File not found":
print("No such file was found. Check again")
continue

elif message == "FileExists":
print("File Already Exists!")
continue

elif message == "Invalid command!":
print("ENTER A VALID COMMAND!")
continue

elif message == "User Invalid":
print("Authentication Unsuccessful!\nTRY AGAIN!")
client.close()

# def main():
# response = input("Press 1 for user login\nPress 2 for admin login\n")

# if response == 1:
# user()
# elif response == 2:
# admin()


user()
124 changes: 124 additions & 0 deletions FTP Server/FTP_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import socket
import os
import threading
import numpy as np

IP_address = socket.gethostbyname(socket.gethostname())
PORT = 5050
#the socket object
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((IP_address,PORT))
server.listen()
print(f"Server is listening on {IP_address}:{PORT}")

clients = []
usernames = np.array(["prasun"])
passwords = np.array(["prasun"])

def sendList(client , address):
files = os.listdir()
files_list = "\n".join(files)
client.send(files_list.encode())

def handle_RETR_command(client, address, filename):
try:
with open(filename, 'rb') as file:
file_content = file.read()
client.send(file_content)
print(f"Sent {filename} to {address}")
except FileNotFoundError:
client.send("File not found".encode())
print(f"File {filename} not found")

def handle_STOR_command(client, address, filename):
files_exist = os.listdir()
files_list = "\n".join(files_exist)
files = files_list.split("\n")

for file in files:
if file == filename:
client.send("FileExists".encode())
return

client.send("OK".encode())
file_content = client.recv(1024)
with open(filename, 'wb') as file:
file.write(file_content)
print(f"File {filename} received from {address} and saved successfully")




def handleValidClient(client , address):
while True:
client.send("Command?".encode())
message = client.recv(1024).decode()
if not message:
continue

command, *args = message.split()

if command == "LIST":
sendList(client, address)
continue

elif command == "STOR":
filename = args[0]
handle_STOR_command(client, address, filename)
continue

elif command == "RETR":
filename = args[0]
handle_RETR_command(client, address, filename)
continue

elif command == "QUIT":
client.close()

# elif command == "ADDUSER":
# new_username = args[0]
# new_password = args[1]
# usernames = np.append(usernames, new_username)
# passwords = np.append(passwords, new_password)

# elif command == "DELUSER":
# toBeremoved = args[0]
# number = np.where(usernames== toBeremoved)[0]
# passwords = np.delete(passwords, number)
# usernames = np.delete(usernames, number)



else:
client.send("Invalid command!".encode())
continue


def AcceptAndAuthenticate():

while True:
client, addressess = server.accept()
print(f"{addressess} clientected to the server")
client.send("User?".encode())
username = client.recv(1024).decode()
client.send("Pass?".encode())
password = client.recv(1024).decode()

# if username == "admin123" and password == "admin123":
# client.send("User valid".encode())
# t = threading.Thread(target=handleValidClient, args=(client , addressess))
# t.start()

#else :
index = np.where(usernames == username)[0]
if passwords[index[0]] == password:
client.send("User valid".encode())
t = threading.Thread(target=handleValidClient, args=(client , addressess))
t.start()

else:
client.send("User Invalid".encode())
client.close()


AcceptAndAuthenticate()
17 changes: 17 additions & 0 deletions FTP Server/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# FTP Server

Implement an FTP server that allows users to authenticate and list, retrieve, and store files.

## Commands

The FTP server should implement the following commands:

- `USER <username>`: This command should be used to authenticate a user. The server should check if the user exists and if it does, ask for the password.
- `PASS <password>`: This command should be used to authenticate a user. The server should check if the password is correct and if it is, allow the user to execute other commands.
- `LIST`: This command should list all the files in the current directory.
- `RETR <filename>`: This command should be used to retrieve a file from the server.
- `STOR <filename>`: This command should be used to store a file on the server.
- `QUIT`: This command should be used to disconnect from the server.

The server should be able to handle multiple clients at the same time.
Also ensure proper error handling. For example, if a user tries to retrieve a file that does not exist, the serve
Loading