This code is written on linux for linux, a few commands need to be changed for windows.
1、Client input IP, port, can server-side connection, was asked to enter the user name and password for verification.
2, the use of independent modules to verify the login user (technology is limited, does not support the client to create a user), username: ftpuser password: userlogin
2、After successful client login verification, you can use ? orhelp to view the commands available.
#!/usr/bin/env python
#-*- coding:utf-8
"Program for ftp server"
from SocketServer import *
from time import *
import os
import loginauth
class MyFtp(StreamRequestHandler):
def handle(self):
try:
while True:
sleep(0.5)
('auth')
name = (BUFSIZ)
sleep(0.5)
('pauth')
password = (BUFSIZ)
print name,password
auth_result = loginauth.user_create(name,password)
print auth_result
if auth_result == 0:
('ok2login')
break
elif auth_result == 1:
('fail2login')
continue
while True:
recv_data = (BUFSIZ).split()
if recv_data[0] == 'rls':
result = ('ls -l ./').read()
(result)
continue
if recv_data[0] == '?' or recv_data[0] == 'help':
send_help = '''\033[32;1m
?\help: Get help.
Get: Downlaod file from remote server.
Send: Send local file to remote server.
ls: List local file.
rls: List remote server file.
quit\exit: Quit the application.
\033[0m'''
(send_help)
continue
if recv_data[0] == 'send':
filename = recv_data[1]
('ok2send')
recv_data = (BUFSIZ)
file2w = open(filename,'wb')
(recv_data)
()
()
('\033[33;1mFile transfer successed!!!\033[0m')
continue
if recv_data[0] == 'get':
filename = recv_data[1]
if (filename):
('ok2get')
if (BUFSIZ) == 'ok2send':
('sending')
sleep(0.5)
file_data = open(filename,'rb')
file_tmp = file_data.read()
(file_tmp)
sleep(1)
('\033[33;1mDownloading complete!\033[0m')
file_data.close()
else:
('fail2get')
if (BUFSIZ) == 'ack':
('\033[31;1m%s not found\033[0m'% filename)
except :
pass
if __name__ == '__main__':
HOST,PORT = '',9889
ADDR = (HOST,PORT)
BUFSIZ = 8192
try:
server = ThreadingTCPServer(ADDR,MyFtp)
server.serve_forever()
except KeyboardInterrupt:
()
#!/usr/bin/env python
#-*- coding:utf-8
#Filename:
"Program for userlogin"
import sys,time
import cPickle as pickle
#If it's your first running this program,use USERDB = {}
#If it is not your first running this program,use USERDB = (open('userdb','rb'))
USERDB = (open('userdb','rb'))
#USERDB = {}
class userdb(object):
def __init__(self,username,password,time):
= username
= password
= time
def save_user(self):
USERDB[] = [,]
(USERDB,open('userdb','wb'),True)
def update_db(self):
pass
def user_create(NAME,PASSWD = ''):
if NAME in USERDB:
if PASSWD == USERDB[NAME][0]:
p = userdb(NAME,PASSWD,())
p.save_user()
return 0
else:
return 1
else:
#p = userdb(NAME,PASSWD,())
#p.save_user()
return 1
if __name__ == '__main__':
user_create(name,password)
#!/usr/bin/env python
#-*- coding:utf-8
"Program for ftp client."
from socket import *
from time import sleep
import os
def auth():
while 1:
try:
recv_msg = (BUFSIZ)
if recv_msg == 'auth':
USER = str(raw_input('Please input your username: ')).strip()
(USER)
if (BUFSIZ) == 'pauth':
PASS = str(raw_input('Please input your password: ')).strip()
(PASS)
recv_msg1 = (BUFSIZ)
if recv_msg1 == 'ok2login':
print '\033[33;1mlogin success!!!\033[0m'
break
elif recv_msg1 == 'fail2login':
print '\033[33;1mlogin failure!!!\033[0m'
continue
else:
continue
except:
return 'error'
def switch():
while True:
INPUT = str(raw_input('ftp> ')).strip()
if len(INPUT) == 0:continue
elif INPUT == 'quit' or INPUT == 'exit':
()
break
elif INPUT == '?' or INPUT == 'help':
(INPUT)
recv_data = (BUFSIZ)
print recv_data
continue
elif INPUT == 'get' or INPUT == 'send':
print '\033[31;1mYou must specified filename!!\033[0m'
continue
elif INPUT == 'ls':
cmd = ('ls -l ./').read()
print cmd
continue
elif INPUT == 'rls':
(INPUT)
recv_data = (BUFSIZ)
print recv_data
continue
elif ()[0] == 'send':
filename = ()[1]
if (filename):
print 'Sending %s......'% filename
(INPUT)
re_data = (BUFSIZ)
if re_data == 'ok2send':
file_data = open(filename,'rb')
file_tmp = file_data.read()
file_data.close()
(file_tmp)
sleep(0.5)
recv_data = (BUFSIZ)
print recv_data
continue
else:continue
else:
print '\033[31;1m%s not found!\033[0m'% filename
elif ()[0] == 'get':
filename = ()[1]
(INPUT)
msg1 = (BUFSIZ)
if msg1 == 'ok2get':
('ok2send')
msg2 = (BUFSIZ)
if msg2 == 'sending':
file_data = (BUFSIZ)
file2w = open(filename,'wb')
(file_data)
()
()
msg3 = (BUFSIZ)
print msg3
continue
elif msg1 == 'fail2get':
('ack')
msg4 = (BUFSIZ)
print msg4
continue
else:
continue
if __name__ == '__main__':
#Default 127.0.0.1
HOST = str(raw_input('Server IP: ')).strip()
#Defautl 9889
PORT = int(raw_input('Server PORT: '))
ADDR = (HOST,PORT)
BUFSIZ = 8192
s = socket(AF_INET,SOCK_STREAM)
try:
(ADDR)
except :
pass
if auth() == 'error':
print 'Connection refused.'
else:
switch()