1. 程式人生 > >python day31--網路程式設計,tcp,udp的指令,及黏包

python day31--網路程式設計,tcp,udp的指令,及黏包

一、TCP

tcp中遇到黏包會讓資料傳輸完。

server

import socket
sk=socket.socket()
sk.bind(('127.0.0.1',8090))
sk.listen()
while True:
    cmd=input('>>>')
    conn,addr=sk.accept()
    conn.send(cmd.encode('utf-8'))
    ret=conn.recv(1024).decode('utf-8')
    print(ret) 
conn.close()
sk.close()

client

import
socket import subprocess sk=socket.socket() sk.connect(('127.0.0.1',8090)) while True: cmd=sk.recv(1024).decode('gbk') ret=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) std_out='stdout:'+(ret.stdout.read()).decode('
gbk') std_err='stderr:'+(ret.stderr.read()).decode('gbk') print(std_err) print(std_out) sk.send(std_out.encode('utf-8')) sk.send(std_err.encode('utf-8')) sk.close()

二、UDP

udp傳輸遇不會黏包。

server

import  socket
sk=socket.socket(type=socket.SOCK_DGRAM)
sk.bind(('127.0.0.1',8090))
msg,addr
=sk.recvfrom(1024) while True: cmd=input('>>>') if cmd=='q':break sk.sendto(cmd.encode('utf-8'),addr) msg,addr=sk.recvfrom(1024) print(msg.decode('utf-8')) sk.close()

client

import  socket
import  subprocess
sk=socket.socket(type=socket.SOCK_DGRAM)
addr=('127.0.0.1',8090)

sk.sendto('吃了嗎'.encode('utf-8'),addr)
while True:
        cmd,addr=sk.recvfrom(1024)
        ret=subprocess.Popen(cmd.decode('gbk'),
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
        std_out='stdout:'+(ret.stdout.read()).decode('gbk')
        std_err='stderr:'+(ret.stderr.read()).decode('gbk')
        print(std_err)
        print(std_out)
        sk.sendto(std_out.encode('utf-8'),addr)
        sk.sendto(std_err.encode('utf-8'),addr)
sk.close()

三、黏包

tcp協議的拆包機制:網路裝置在傳輸時會把資料拆成幾塊來傳輸,這樣會產生資料碎片,從而造成資料丟失

tcp黏包:傳送方引起的粘包是由TCP協議本身造成的,TCP為提高傳輸效率,傳送方往往要收集到足夠多的資料後才傳送一個TCP段。若連續幾次需要send的資料都很少,通常TCP會根據優化演算法把這些資料合成一個TCP段後一次傳送出去,這樣接收方就收到了粘包資料。

四、網盤作業

server

import socket
sk=socket.socket()
sk.bind(('127.0.0.1',8080))
sk.listen()
conn,addr=sk.accept()
user=conn.recv(1024).decode('utf-8')
pwd=conn.recv(1024).decode('utf-8')
print(user,pwd)
while True:
    if user=='alex' and pwd=='123456':
        conn.send(bytes('1.上傳檔案  2.下載檔案',encoding='utf-8'))
        shell=conn.recv(10240).decode('utf-8')
        print(shell)
        if shell== '1':
            txt=conn.recv(10240).decode('utf-8')
            with open('a.txt','w',encoding='utf-8') as f1:
                f1.write(txt)
                conn.send(bytes('上傳成功',encoding='utf-8'))
                break
        elif shell== '2':
            with open('b.txt','r',encoding='utf-8') as f1:
                conn.send(bytes(f1.read(), encoding='utf-8'))
                conn.send(bytes('下載成功',encoding='utf-8'))
                break
conn.close()
sk.close()

client

import socket
sk=socket.socket()
sk.connect(('127.0.0.1',8080))
user=input('輸入使用者名稱:')
pwd=input('輸入密碼:')
sk.send(bytes(user,encoding='utf-8'))
sk.send(bytes(pwd,encoding='utf-8'))
shell=sk.recv(1024).decode('utf-8')
print(shell)
cmd=input('>>>')
sk.send(bytes(cmd,encoding='utf-8'))
while True:
    if cmd=='1':
        with open('a1.txt','r',encoding='utf-8') as f1:
            sk.send(bytes(f1.read(),encoding='utf-8'))
            msg=sk.recv(10240).decode('utf-8')
            print(msg)
            break
    elif cmd=='2':
        with open('C:/Users/lenovo/Desktop/111/b1.txt','w',encoding='utf-8') as f1:
            info=sk.recv(10240).decode('utf-8')
            msg=sk.recv(10240).decode('utf-8')
            f1.write(info)
            print(msg)
            break
sk.close()