网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

初试python的socket编程--ftp

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()


网站标题:初试python的socket编程--ftp
文章源于:http://bjjierui.cn/article/gsdgco.html

其他资讯