客户端服务端

客户端

"""
file: send.py
socket client
"""

import socket
import os
import sys
import struct
import time 

def socket_client():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(('127.0.0.1', 6666))
    except socket.error as msg:
        print msg
        sys.exit(1)

    print s.recv(1024)

    
    filepath = raw_input('please input file path: ')
    if os.path.isfile(filepath):
            # 定义定义文件信息。128s表示文件名为128bytes长,l表示一个int或log文件类型,在此为文件大小
        fileinfo_size = struct.calcsize('128sl')
            # 定义文件头信息,包含文件名和文件大小
        fhead = struct.pack('128sl', os.path.basename(filepath),
                                os.stat(filepath).st_size)
        s.send(fhead)
        print 'client filepath: {0}'.format(filepath)

        fp = open(filepath, 'rb')
        while 1:
                #可以按照指定的大小分割文件为一系列子文件。
            data = fp.read(1024)
            if not data:
                print '{0} file send over...'.format(filepath)
                break
            s.send(data)
            
       
    fileinfo0_size = struct.calcsize('128sl')
    buf0 = s.recv(fileinfo0_size)
    if buf0:
        filename0, filesize0 = struct.unpack('128sl', buf0)
        fn0 = filename0.strip('\00')
        new_filename0 = os.path.join('./', 'new1_' + fn0)            
        fp = open(new_filename0, 'wb')
        recvd_size0=0
        while not recvd_size0 == filesize0:
            if filesize0 - recvd_size0 > 1024:
                data1 = s.recv(1024)
                recvd_size0 += len(data1)
            else:
                data1 = s.recv(filesize0 - recvd_size0)
                recvd_size0 = filesize0
            fp.write(data1)      
                
    s.close()
        


if __name__ == '__main__':
    socket_client()

服务端

import socket
import threading
import time
import sys
import os
import struct
import cv2


def socket_service():
    try:
        #创建套接字,绑定套接字到本地IP与端口
        #socket.SOCK_STREAM,流式socket , for TCP
        #socket.AF_INET,服务器之间网络通信IPv4
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind(('', 6666))
        s.listen(10)
    except socket.error as msg:
        print msg
        sys.exit(1)
    print 'Waiting connection...'

    while 1:
        conn, addr = s.accept()
        #创建线程
        t = threading.Thread(target=deal_data, args=(conn, addr))
        t.start()

def deal_data(conn, addr):
    print 'Accept new connection from {0}'.format(addr)
    #conn.settimeout(500)
    conn.send('Hi, Welcome to the server!')
    
    while 1:
        #struct.calcsize用于计算格式字符串所对应的结果的长度
        #128s表示文件名为128bytes长,l表示一个int或log文件类型,在此为文件大小
        fileinfo_size = struct.calcsize('128sl')
        #conn.recv接收一个信息,并指定接收的大小 为文件大小
        buf = conn.recv(fileinfo_size)
        if buf:
            #struct.unpack按照给定的格式(fmt)解析字节流string,返回解析出来的tuple
            filename, filesize = struct.unpack('128sl', buf)
            print filename
            fn = filename.strip('\00')
            new_filename = os.path.join('./', 'new_' + fn)
            print 'file new name is {0}, filesize if {1}'.format(new_filename,
                                                                 filesize)

            recvd_size = 0  # 定义已接收文件的大小
            fp = open(new_filename, 'wb')
            print 'start receiving...'

            while not recvd_size == filesize:
                if filesize - recvd_size > 1024:
                    data = conn.recv(1024)
                    recvd_size += len(data)
                else:
                    data = conn.recv(filesize - recvd_size)
                    recvd_size = filesize
                fp.write(data)    

            fp.close()
            print 'end receive...'
            break
        
    img=cv2.imread(new_filename)
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    new_filename1 = os.path.join('./', 'new0_' + fn)
    cv2.imwrite(new_filename1,gray)
    new_filename1_size = struct.calcsize('128sl')
            # 定义文件头信息,包含文件名和文件大小
    fhead1 = struct.pack('128sl', os.path.basename(new_filename1),
                                os.stat(new_filename1).st_size)
    conn.send(fhead1)             
    fp1=open(new_filename1, 'rb')
    print 'start sending...'
    while 1:
                #可以按照指定的大小分割文件为一系列子文件。
        data0 = fp1.read(1024)
        if not data0:
            print '{0} file send over...'.format(new_filename1)
            break
        conn.send(data0)
            
            
            
    conn.close()
        


if __name__ == '__main__':
    socket_service()

猜你喜欢

转载自blog.csdn.net/qiqi__xu/article/details/89445687