前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python socket 进行文件上

python socket 进行文件上

作者头像
py3study
发布2020-01-10 17:22:50
4110
发布2020-01-10 17:22:50
举报
文章被收录于专栏:python3python3

!/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"]

代码语言:javascript
复制
        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' )

代码语言:javascript
复制
            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()

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • !/bin/python
  • if name == 'main': server = SocketServer.ThreadingTCPServer(('192.168.221.203',8888),server) server.serve_forever()
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档