python socket 进行文件上

发布时间:2019-08-05 14:59:52编辑:auto阅读(1355)

    !/bin/python

    #coding:utf-8
    import SocketServer
    import os
    import datetime
    import MySQLdb
    class mysql:
    def init(self):
    self.connect = MySQLdb.connect(
    host = '192.168.221.203',
    user = 'hall',
    passwd = '520157',
    port = 3306,
    db = 'info',
    charset = 'utf8'
    )
    def mysql_create_table_info(self):
    cursor = self.connect.cursor()
    sql = """create table if not exists info(
    action char(20),
    actin_time varchar(40),
    file_size varchar(20),
    file_name varchar(40),
    operation_user char(20)
    )"""
    cursor.execute(sql)
    cursor.close()
    self.connect.commit()
    self.connect.close()
    def mysql_create_table_user(self):
    cursor = self.connect.cursor()
    sql_user = """create table if not exists user(
    user varchar(20),
    passwd varchar(40)
    )"""
    cursor.execute(sql_user)
    data = """insert into user values(
    "hall",
    "hall"),
    ("hexulin",
    "hexulin")"""
    cursor.execute(data)
    cursor.close()
    self.connect.commit()
    self.connect.close()

    class server(SocketServer.BaseRequestHandler,mysql):
    def handle(self):
    self.request
    self.client_address
    print "%s:%s is connecting....."%self.client_address
    recv = self.request.recv(1024)
    print recv
    self.request.send("已经建立连接")
    recv = self.request.recv(1024)
    print recv
    operation = recv.split(':')[1]
    if operation == "put":
    self.file_name,self.file_size = self.request.recv(1024).split('|')
    self.request.send("一切准备就绪")
    recv_size = 0
    time_put = datetime.datetime.now()
    file_path = ['/root/',self.file_name]
    file_path = ''.join(file_path)
    f = open(file_path,'wb')
    while recv_size != int(self.file_size):
    if int(self.file_size) - recv_size > 1024:
    rdata = self.request.recv(1024)
    recv_size += len(rdata)
    else:
    rdata = self.request.recv(int(self.file_size) - recv_size)
    recv_size = int(self.file_size)
    f.write(rdata)
    f.close()
    print "文件已经保存完毕...."
    connect = MySQLdb.connect(
    host = '192.168.221.203',
    user = 'hall',
    passwd = '520157',
    port = 3306,
    db = 'info',
    charset = 'utf8'
    )
    value = [operation,time_put,self.file_size,self.file_name,"hall"]

            cursor = connect.cursor()
                        data = """insert into info values(
                                %s,
                                %s,
                                %s,
                                %s,
                %s)"""
                        cursor.execute(data,value)
                        cursor.close()
                        connect.commit()
                        connect.close()
    
        else:
            file_path = self.request.recv(1024)
                    file_name = os.path.basename(file_path)
                    file_size = os.stat(file_path).st_size
                    self.request.send(file_name + '|' + str(file_size))
                    recv = self.request.recv(1024)
                    print recv
                    send_size = 0
            time_get = datetime.datetime.now()
                    f = open(file_path,'rb')
                    while True:
                            file_data = f.read(1024)
                            if not file_data:
                                    break
                            self.request.send(file_data)
                    f.close()
            print "文件已经传输完毕..."
            connect = MySQLdb.connect(
                    host = '192.168.221.203',
                    user = 'hall',
                    passwd = '520157',
                    port = 3306,
                    db = 'info',
                    charset = 'utf8'
                    )
            value = [operation,time_get,file_size,file_name,"hall"]
            cursor = connect.cursor()
                        data = """insert into info values(
                                %s,
                                %s,
                                %s,
                                %s,
                %s)"""
                        cursor.execute(data,value)
                        cursor.close()
                        connect.commit()
                        connect.close()

    class insert_mysql:
    def mysql_info(self):
    connect = MySQLdb.connect(
    host = '192.168.221.203',
    user = 'hall',
    passwd = '520157',
    port = 3306,
    db = 'info',
    charset = 'utf8'
    )

                cursor = connect.cursor()
                data = """insert into info values(
                       operation,
                       time),
                       self.file_size,
                       self.file_name)"""
                cursor.execute(data)
                cursor.close()
                connect.commit()
                connect.close()

    if name == 'main':
    server = SocketServer.ThreadingTCPServer(('192.168.221.203',8888),server)
    server.serve_forever()

    客户端
    #!/bin/python
    #coding:utf-8
    import socket
    import os

    sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.connect(('192.168.221.203',8888))
    sock.send("我要连接你进行文件传输........")
    recv = sock.recv(1024)
    print recv
    operation = raw_input("please input your choice Usage: put or get:")
    sock.send("我要进行的操作是:%s"%operation)
    if operation == "put":
    file_path = raw_input("please input your input_file_path:")
    file_name = os.path.basename(file_path)
    file_size = os.stat(file_path).st_size
    print file_size
    sock.send(file_name + '|' + str(file_size))
    print sock.recv(1024)
    f = open(file_path,'rb')
    while True:
    file_data = f.read(1024)
    if not file_data:
    break
    sock.send(file_data)
    f.close()
    print "上传文件完毕....."
    else:
    file_path = raw_input("please input your get_file_path:")
    sock.send(file_path)
    file_name,file_size = sock.recv(1024).split('|')
    get_file_path = ['/root/',file_name]
    get_file_path = ''.join(get_file_path)
    sock.send("可以开始发送,准备就绪")
    recv_size = 0
    f = open(get_file_path,'wb')
    while not int(file_size) == recv_size:
    if int(int(file_size) - recv_size) > 1024:
    rdata = sock.recv(1024)
    recv_size += len(rdata)
    else:
    rdata = sock.recv(int(file_size) - recv_size)
    recv_size = int(file_size)
    f.write(rdata)
    f.close()
    print "文件下载完毕....."
    sock.close()

关键字