欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3實(shí)現(xiàn)ftp服務(wù)功能(客戶端)

 更新時(shí)間:2017年03月24日 16:28:09   作者:想自由  
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)ftp服務(wù)功能,客戶端的相應(yīng)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python3實(shí)現(xiàn)ftp服務(wù)功能的具體代碼,供大家參考,具體內(nèi)容如下

客戶端 main代碼:

#Author by Andy
#_*_ coding:utf-8 _*_
'''
This program is used to create a ftp client

'''
import socket,os,json,time,hashlib,sys
class Ftp_client(object):
 def __init__(self):
  self.client = socket.socket()
 def help(self):
  msg = '''useage:
  ls
  pwd
  cd dir(example: / .. . /var)
  put filename
  rm filename
  get filename
  mkdir directory name
  '''
  print(msg)
 def connect(self,addr,port):
  self.client.connect((addr,port))
 def auth(self):
  m = hashlib.md5()
  username = input("請(qǐng)輸入用戶名:").strip()

  m.update(input("請(qǐng)輸入密碼:").strip().encode())
  password = m.hexdigest()
  user_info = {
   'action':'auth',
   'username':username,
   'password':password}
  self.client.send(json.dumps(user_info).encode('utf-8'))
  server_response = self.client.recv(1024).decode()
  # print(server_response)
  return server_response
 def interactive(self):
  while True:
   msg = input(">>>:").strip()
   if not msg:
    print("不能發(fā)送空內(nèi)容!")
    continue
   cmd = msg.split()[0]
   if hasattr(self,cmd):
    func = getattr(self,cmd)
    func(msg)
   else:
    self.help()
    continue
 def put(self,*args):
  cmd_split = args[0].split()
  if len(cmd_split) > 1:
   filename = cmd_split[1]
   if os.path.isfile(filename):
    filesize = os.stat(filename).st_size
    file_info = {
     "action":"put",
     "filename":filename,
     "size":filesize,
     "overriding":'True'
    }
    self.client.send( json.dumps(file_info).encode('utf-8') )
    #防止粘包,等待服務(wù)器確認(rèn)。
    request_code = {
     '200': 'Ready to recceive data!',
     '210': 'Not ready to received data!'
    }
    server_response = self.client.recv(1024).decode()
    if server_response == '200':
     f = open(filename,"rb")
     send_size = 0
     start_time = time.time()
     for line in f:
      self.client.send(line)
      send_size += len(line)
      send_percentage = int((send_size / filesize) * 100)
      while True:
       progress = ('\r已上傳%sMB(%s%%)' % (round(send_size / 102400, 2), send_percentage)).encode(
        'utf-8')
       os.write(1, progress)
       sys.stdout.flush()
       time.sleep(0.0001)
       break
     else:
      end_time = time.time()
      time_use = int(end_time - start_time)
      print("\nFile %s has been sent successfully!" % filename)
      print('\n平均下載速度%s MB/s' % (round(round(send_size / 102400, 2) / time_use, 2)))
      f.close()
    else:
     print("Sever isn't ready to receive data!")
     time.sleep(10)
     start_time = time.time()
     f = open(filename, "rb")
     send_size = 0
     for line in f:
       self.client.send(line)
       send_size += len(line)
       # print(send_size)
       while True:
        send_percentage = int((send_size / filesize) * 100)
        progress = ('\r已上傳%sMB(%s%%)' % (round(send_size / 102400, 2), send_percentage)).encode(
         'utf-8')
        os.write(1, progress)
        sys.stdout.flush()
        # time.sleep(0.0001)
        break
     else:
      end_time = time.time()
      time_use = int(end_time - start_time)
      print("File %s has been sent successfully!" % filename)
      print('\n平均下載速度%s MB/s' % (round(round(send_size / 102400, 2) / time_use, 2)))
      f.close()
   else:
    print("File %s is not exit!" %filename)
  else:
   self.help()
 def ls(self,*args):
  cmd_split = args[0].split()
  # print(cmd_split)
  if len(cmd_split) > 1:
   path = cmd_split[1]
  elif len(cmd_split) == 1:
   path = '.'
  request_info = {
   'action': 'ls',
   'path': path
  }
  self.client.send(json.dumps(request_info).encode('utf-8'))
  sever_response = self.client.recv(1024).decode()
  print(sever_response)
 def pwd(self,*args):
  cmd_split = args[0].split()
  if len(cmd_split) == 1:
   request_info = {
    'action': 'pwd',
   }
   self.client.send(json.dumps(request_info).encode("utf-8"))
   server_response = self.client.recv(1024).decode()
   print(server_response)
  else:
   self.help()
 def get(self,*args):
  cmd_split = args[0].split()
  if len(cmd_split) > 1:
   filename = cmd_split[1]
   file_info = {
     "action": "get",
     "filename": filename,
     "overriding": 'True'
    }
   self.client.send(json.dumps(file_info).encode('utf-8'))
   server_response = self.client.recv(1024).decode() #服務(wù)器反饋文件是否存在
   self.client.send('0'.encode('utf-8'))
   if server_response == '0':
    file_size = int(self.client.recv(1024).decode())
    # print(file_size)
    self.client.send('0'.encode('utf-8')) #確認(rèn)開始傳輸數(shù)據(jù)
    if os.path.isfile(filename):
     filename = filename+'.new'
    f = open(filename,'wb')
    receive_size = 0
    m = hashlib.md5()
    start_time = time.time()
    while receive_size < file_size:
     if file_size - receive_size > 1024: # 還需接收不止1次
      size = 1024
     else:
      size = file_size - receive_size
     data = self.client.recv(size)
     m.update(data)
     receive_size += len(data)
     data_percent=int((receive_size / file_size) * 100)
     f.write(data)
     progress = ('\r已下載%sMB(%s%%)' %(round(receive_size/102400,2),data_percent)).encode('utf-8')
     os.write(1,progress)
     sys.stdout.flush()
     time.sleep(0.0001)

    else:
     end_time = time.time()
     time_use = int(end_time - start_time)
     print('\n平均下載速度%s MB/s'%(round(round(receive_size/102400,2)/time_use,2)))
     Md5_server = self.client.recv(1024).decode()
     Md5_client = m.hexdigest()
     print('文件校驗(yàn)中,請(qǐng)稍候...')
     time.sleep(0.3)
     if Md5_server == Md5_client:
      print('文件正常。')
     else:
      print('文件與服務(wù)器MD5值不符,請(qǐng)確認(rèn)!')
   else:
    print('File not found!')
    client.interactive()
  else:
   self.help()
 def rm(self,*args):
  cmd_split = args[0].split()
  if len(cmd_split) > 1:
   filename = cmd_split[1]
   request_info = {
    'action':'rm',
    'filename': filename,
    'prompt':'Y'
   }
   self.client.send(json.dumps(request_info).encode("utf-8"))
   server_response = self.client.recv(10240).decode()
   request_code = {
    '0':'confirm to deleted',
    '1':'cancel to deleted'
   }

   if server_response == '0':
    confirm = input("請(qǐng)確認(rèn)是否真的刪除該文件:")
    if confirm == 'Y' or confirm == 'y':
     self.client.send('0'.encode("utf-8"))
     print(self.client.recv(1024).decode())
    else:
     self.client.send('1'.encode("utf-8"))
     print(self.client.recv(1024).decode())
   else:
    print('File not found!')
    client.interactive()


  else:
   self.help()
 def cd(self,*args):
  cmd_split = args[0].split()
  if len(cmd_split) > 1:
   path = cmd_split[1]
  elif len(cmd_split) == 1:
   path = '.'
  request_info = {
   'action':'cd',
   'path':path
  }
  self.client.send(json.dumps(request_info).encode("utf-8"))
  server_response = self.client.recv(10240).decode()
  print(server_response)
 def mkdir(self,*args):
  request_code = {
   '0': 'Directory has been made!',
   '1': 'Directory is aleady exist!'
  }
  cmd_split = args[0].split()
  if len(cmd_split) > 1:
   dir_name = cmd_split[1]
   request_info = {
    'action':'mkdir',
    'dir_name': dir_name
   }
   self.client.send(json.dumps(request_info).encode("utf-8"))
   server_response = self.client.recv(1024).decode()
   if server_response == '0':
    print('Directory has been made!')
   else:
    print('Directory is aleady exist!')
  else:
   self.help()
 # def touch(self,*args):
def run():
 client = Ftp_client()
 # client.connect('10.1.2.3',6969)
 Addr = input("請(qǐng)輸入服務(wù)器IP:").strip()
 Port = int(input("請(qǐng)輸入端口號(hào):").strip())
 client.connect(Addr,Port)
 while True:
  if client.auth() == '0':
   print("Welcome.....")
   client.interactive()
   break
  else:
   print("用戶名或密碼錯(cuò)誤!")
   continue

目錄結(jié)構(gòu):

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python使用QQ郵箱實(shí)現(xiàn)自動(dòng)發(fā)送郵件

    python使用QQ郵箱實(shí)現(xiàn)自動(dòng)發(fā)送郵件

    這篇文章主要為大家詳細(xì)介紹了python使用QQ郵箱實(shí)現(xiàn)自動(dòng)發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • python pyheatmap包繪制熱力圖

    python pyheatmap包繪制熱力圖

    這篇文章主要為大家詳細(xì)介紹了python pyheatmap包繪制熱力圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Python多分支語句的三種結(jié)構(gòu)詳解

    Python多分支語句的三種結(jié)構(gòu)詳解

    這篇文章主要介紹了Python多分支語句的三種結(jié)構(gòu),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python完全卸載三種方法教程

    python完全卸載三種方法教程

    通常我們?cè)谛遁dpyhton時(shí)會(huì)直接使用電腦自備的管家軟件中的卸載功能,但是通常這并不會(huì)卸載干凈,特別是當(dāng)你卸載完python一個(gè)版本之后,重新安裝另一個(gè)版本就會(huì)出錯(cuò),這篇文章主要給大家介紹了關(guān)于python完全卸載三種方法的相關(guān)資料,需要的朋友可以參考下
    2023-09-09
  • 詳解python中return和print的區(qū)別和用途

    詳解python中return和print的區(qū)別和用途

    在 Python 中,return 和 print 是兩種常見的語句,用于在函數(shù)中輸出信息或返回值,盡管它們看起來相似,但它們有不同的作用和用法,本文將詳細(xì)介紹 return 和 print 在函數(shù)中的區(qū)別,并提供豐富的示例代碼,以幫助你更好地理解它們的用途
    2023-11-11
  • python K近鄰算法的kd樹實(shí)現(xiàn)

    python K近鄰算法的kd樹實(shí)現(xiàn)

    這篇文章主要介紹了python K近鄰算法的kd樹實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • python實(shí)現(xiàn)從一組顏色中找出與給定顏色最接近顏色的方法

    python實(shí)現(xiàn)從一組顏色中找出與給定顏色最接近顏色的方法

    這篇文章主要介紹了python實(shí)現(xiàn)從一組顏色中找出與給定顏色最接近顏色的方法,涉及Python操作rgb格式顏色的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • Python數(shù)據(jù)分析之matplotlib繪圖詳解

    Python數(shù)據(jù)分析之matplotlib繪圖詳解

    這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)分析之如何利用matplotlib進(jìn)行繪圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-09-09
  • Python實(shí)戰(zhàn)使用XPath采集數(shù)據(jù)示例解析

    Python實(shí)戰(zhàn)使用XPath采集數(shù)據(jù)示例解析

    這篇文章主要為大家介紹了Python實(shí)戰(zhàn)之使用XPath采集數(shù)據(jù)實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>
    2023-04-04
  • 用Python給二維碼圖片添加提示文字

    用Python給二維碼圖片添加提示文字

    今天教各位小伙伴怎么用Python給二維碼圖片添加提示文字,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴很有幫助,需要的朋友可以參考下
    2021-05-05

最新評(píng)論