python socket 進行檔案上傳下載

來源:互聯網
上載者:User

標籤:[1]   eve   socket   value   strong   操作   handle   ati   class   

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

python socket 進行檔案上傳下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.