python怎么使用socket通信傳輸數(shù)據(jù)-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關python怎么使用socket通信傳輸數(shù)據(jù)的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

站在用戶的角度思考問題,與客戶深入溝通,找到靜安網(wǎng)站設計與靜安網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設計、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、主機域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務覆蓋靜安地區(qū)。

1.服務器端

功能:計算客戶端傳輸過來的文件個數(shù)、文件總長度、字符串總次數(shù)、字符串總個數(shù)、數(shù)據(jù)總長度,并且總長度不大于max_len,否則服務器將斷開聯(lián)系

異常處理

import socket
server=socket.socket()
server.bind(("localhost",5555))
server.listen()
conn,address=server.accept()
 
file_number=0#文件個數(shù)
file_len=0#文件總字長
data_number=0#字符串個數(shù)
data_len=0#數(shù)據(jù)長度
recv_len=0#總字長
max_len=2000000
 
#文件處理方法
def file_deal(data,file_number):
    with open(str(file_number)+'.jpg','wb')as f:
        f.write(data)
    print("文件保存成功!")
 
 
while True:
    try:
        #傳輸數(shù)據(jù)超過max_len則斷開數(shù)據(jù)傳輸
        if recv_len>max_len:
            print("客戶端傳輸數(shù)據(jù)超過{}字節(jié),服務器斷開連接".format(max_len))
            print("本次一共傳輸{}字節(jié)的數(shù)據(jù)那,其中字符串為{}字節(jié),傳輸{}次,文件為有{}字節(jié),傳輸{}次".format(recv_len, data_len, data_number, file_len, file_number))
            #conn.send("你已經(jīng)超出傳輸范圍{},服務器斷開連接".format(max_len))
            break
        recv_data=conn.recv(1000)
        #recv處理層!
        #服務器和客戶端達成協(xié)議,客戶端向服務器端發(fā)來的指令:
        # 發(fā)送文件為2,發(fā)送數(shù)據(jù)為1,結束發(fā)送為4,繼續(xù)發(fā)送為3
        #send處理層:
        #回復1為收到,2為未收到!
        if recv_data.decode('utf-8')=='1':
            print("準備客戶端發(fā)送過來的字符串!")
            data_recv=conn.recv(1000)
            print("收到從客戶端發(fā)送過來的字符串:",data_recv.decode('utf-8'))
            data_number+=1#字符串總個數(shù)
            recv_len+=len(data_recv)#總字長
            data_len+=len(data_recv)#字符串總長
        elif recv_data.decode('utf-8')=='2':
            print("準備接受客戶端發(fā)送過來的文件")
            data_recv=conn.recv(100000000)
            file_deal(data_recv,file_number)
            file_number+=1#文件個數(shù)
            recv_len += len(data_recv)#總字長
            file_len+=len(data_recv)#文件總字長
        elif recv_data.decode('utf-8')=='3':
            print("客戶端重新發(fā)送數(shù)據(jù)")
            data_recv=conn.recv(10000)
            continue
        else:
            print("客戶端已斷開聯(lián)系!我也斷開聯(lián)系吧")
            data_recv=conn.recv(10000)
            print("本次一共傳輸{}字節(jié)的數(shù)據(jù)那,其中字符串為{}字節(jié),傳輸{}次,文件為有{}字節(jié),傳輸{}次".format(recv_len, data_len, data_number, file__len, file_number))
            break
    except:
        print("客戶端突然中斷")
        break
server.close()

2.客戶端

功能:向服務器端發(fā)送文件和字符串,并進行異常處理

import socket,os
 
def isfile(filename):
    result=os.path.exists(filename)
    return result
 
def readfile(filename):
    with open(filename,'rb')as f:
        data=f.read()
    return data
 
specialchar=['1','2','3','4']
client=socket.socket()
client.connect(('localhost',5555))
 
while True:
    try:
        #if client.recv(1000):
            #break
        data_send=input("請輸入您想發(fā)送的數(shù)據(jù)類型!1為字符串2為文件4為結束發(fā)送3為繼續(xù)發(fā)送:").strip()
        if data_send not in specialchar:
            continue
        client.send(data_send.encode('utf-8'))
        if data_send=='1':
            while True:
                data_send=input("請輸入您想發(fā)送的字符串:").strip()
                if data_send not in specialchar:
                    client.send(data_send.encode('utf-8'))
                    break
                else:
                    continue
        elif data_send=='2':
            while True:
                data_filename=input("請您輸入你想發(fā)送的文件名:").strip()
                if isfile(data_filename):
                    client.send(readfile(data_filename))
                    break
                else:
                    print("請重新輸入文件!")
                    continue
        elif data_send=='3':
            client.send('3'.encode('utf-8'))
            continue
        elif data_send=='4':
            client.send('4'.encode('utf-8'))
            break
        else:
            print("輸入錯誤!請重新輸入!")
            continue
    except:
        print("服務器端突然中斷!")
        break
 
client.close()

實現(xiàn)功能:實時傳輸數(shù)據(jù)(包括字符串、文件),客戶端文件的保存等。

3.socket通信優(yōu)點

客戶端:

向服務器端發(fā)送文件和字符串,并進行異常處理

服務器端:

計算客戶端傳輸過來的文件個數(shù)、文件總長度、字符串總次數(shù)、字符串總個數(shù)、數(shù)據(jù)總長度,并且總長度不大于max_len,否則服務器將斷開聯(lián)系、異常處理。

感謝各位的閱讀!關于“python怎么使用socket通信傳輸數(shù)據(jù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

當前題目:python怎么使用socket通信傳輸數(shù)據(jù)-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://muchs.cn/article38/dphipp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、微信小程序、企業(yè)網(wǎng)站制作、關鍵詞優(yōu)化、網(wǎng)頁設計公司品牌網(wǎng)站設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設