socket实现FTP

FTP客户端:
这块应该是在数据库里面存储:用户名,密码  这里测试就暂时用一个字典:
jesn@jesn-virtual-machine:~$ cat socket_Client.py
import socket, time
HOST = 'localhost'
PORT = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
dirc = {'jesn':110, 'mary':120}
def check_user():
        while 1:
                user = raw_input('please enter your user').strip()
                if user in dirc.keys():
                        count = 0
                        while 1:
                                if count <3:
                                        passwd = int(raw_input('please enter your passwd').strip())
                                        if passwd == dirc[user]:
                                                print 'welcome in my ftp!'
                                                break
                                        else:
                                                print 'error passwd!!! please enter again!'
                                                count = count +1
                                                continue
                else:
                        print 'username not in the list!!!'
                        continue
                break
check_user()
while 1:
        Input = raw_input('Please input the command :').strip()
        if len(Input) == 0:
                continue
        s.sendall(Input)
        user_input = Input.split()
        if user_input[0] == 'get':
                with open(user_input[1],'wr') as f:
                        f.write(s.recv(1024))
                        print 'getting ...'
                if s.recv(1024) == 'ftpisdown':continue
                if not s.recv(1024):break
        if user_input[0] == 'set':
                with open(user_input[1],'rb') as f:
                        y = f.read()
                        s.sendall(y)
                        print 'senting...'
                if s.recv(1024) == 'receiveDown':continue
                if not s.recv(1024):break
        data = s.recv(8192)
        time.sleep(2)
        #print 'Received', repr(data)
        print data
s.close()
jesn@jesn-virtual-machine:~$
FTP 服务器端:
jesn@jesn-virtual-machine:~/ftp$ cat ../thread_socket_server.py
import SocketServer,commands,time
class MyTCPHandler(SocketServer.BaseRequestHandler):
        def handle(self):
                while 1:
                        self.data = self.request.recv(1024).strip()
                        print "{} wrote:".format(self.client_address[0])
                        if not self.data:
                                print "client %s is dead!"%self.client_address[0]
                                break
                        user_input = self.data.split()
                        print user_input[0]
                        if user_input[0] == 'get':
                                with open(user_input[1],'rb') as f:
                                        y = f.read()
                                        self.request.sendall(y)
                                        time.sleep(2)
                                        self.request.sendall('ftpisdown')
                                        continue
                        if user_input[0] == 'set':
                                with open(user_input[1],'wb') as f:
                                        y = self.request.recv(1024)
                                        f.write(y)
                                        print 'writeing...'
                                        self.request.sendall('receiveDown')
                                        continue
                        comman_statu,comman_test = commands.getstatusoutput(self.data)
                        if len(comman_test) != 0:
                                self.request.sendall(comman_test)
                                print comman_test
                        else:
                                self.request.sendall('Done')
if __name__ == "__main__":
        HOST, PORT = "localhost",9999
        server = SocketServer.ThreadingTCPServer((HOST,PORT), MyTCPHandler)
        server.serve_forever()
jesn@jesn-virtual-machine:~/ftp$


你可能感兴趣的:(socket,python)