91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

初試python的socket編程--ftp

發布時間:2020-08-18 11:50:05 來源:ITPUB博客 閱讀:192 作者:lsq_008 欄目:編程語言

server端:

 #_*_coding:utf-8_*_
 
import SocketServer
import os
import commands

class MyTCPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        while True:
            instruction = self.request.recv(1024).strip() #接收客戶端命令
            if not instruction:break
            instruction = instruction.split('|')
            if hasattr(self,instruction[0]):
                func = getattr(self,instruction[0])
                func(instruction)
    def FileTransfer(self,msg):
        print '----filetransfer---',msg
        if msg[1] == 'get':
            print "clients wants to download file:",msg[2]
            if os.path.isfile(msg[2]):
                file_size = os.path.getsize(msg[2])
                res = "ready|%s" % file_size
            else:
                res = "file doesn't exist"
            print 'res=====',res
            self.send_confirmation = "FileTransfer|get|%s" %res
            self.request.send(self.send_confirmation)
            feedback = self.request.recv(100)
            if feedback == 'FileTransfer|get|recv_ready':
                f = file(msg[2],'rb')
                send_size = 0
                while not file_size == send_size:
                    if file_size - send_size > 1024:
                        data = f.read(1024)
                        send_size += 1024
                    else:
                       data = f.read(file_size - send_size)
                       send_size += (file_size - send_size)
                    self.request.send(data)
                    print file_size,send_size
            elif msg[1] == 'put':
                pass
    def ls(self,msg):
        print msg
        cmd_result = commands.getstatusoutput(msg[0])
        self.request.sendall(cmd_result[1])
if __name__ == '__main__':
    HOST,PORT = "",9002
    server = SocketServer.ThreadingTCPServer((HOST,PORT),MyTCPHandler)
    server.serve_forever()


client端:

#_*_coding:utf-8_*_

import socket
import os

class FtpClient(object):
    def __init__(self,host,port):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((host,port))
    def start(self):
        self.interactive()

    def interactive(self):
        while True:
            user_input = raw_input(">>:").strip()
            if len(user_input) == 0:continue
            user_input = user_input.split()
            
            if hasattr(self,user_input[0]):
                func = getattr(self,user_input[0])
                func(user_input)
            else:
                print "wrong command usage"

    def get(self,msg):
        print '--get func---',msg
        if len(msg) == 2:
            file_name = msg[1]            
            instruction = "FileTransfer|get|%s" % file_name
            print 'instruction====',instruction
            self.sock.send(instruction)
            feedback = self.sock.recv(100)
            print '--->',feedback
            if feedback.startswith("FileTransfer|get|ready"):
                file_size = int(feedback.split("|")[-1])
                self.sock.send("FileTransfer|get|recv_ready") #告訴服務器端已經準備好了接收
                recv_size = 0
                f = file('client_recv/%s' % os.path.basename(file_name),'wb')
                print '---->',file_name
                while not file_size == recv_size:
                    if file_size - recv_size > 1024:
                        data = self.sock.recv(1024)
                        recv_size += len(data)
                    else:
                        data = self.sock.recv(file_size - recv_size)
                        recv_size += (file_size - recv_size)
                    f.write(data)
                    print file_size,recv_size
                else:
                    print '---recv file:%s---' % file_name
                    f.close()
            else:
                print feedback
        else:
            print "wrong command usage!"
    def ls(self,msg):
        if len(msg) == 1:
            self.sock.send(msg[0])
            result = self.sock.recv(1024)
            print result if __name__ == '__main__':
    f = FtpClient('localhost',9002)
    f.start()

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

科技| 独山县| 芦山县| 天台县| 瑞安市| 宁都县| 庆安县| 霸州市| 油尖旺区| 荃湾区| 铜梁县| 衡水市| 南川市| 岳普湖县| 来宾市| 汽车| 金华市| 潼关县| 东山县| 雅江县| 宁河县| 公安县| 合江县| 平安县| 久治县| 潞西市| 米脂县| 兴业县| 佛山市| 吉首市| 确山县| 长葛市| 清丰县| 霍城县| 灌阳县| 政和县| 临夏县| 旺苍县| 凌海市| 宁南县| 宁武县|