python-20-優化socket_tcp

weixin_33797791發表於2018-03-08

struck

import struct
#b'\x0c\x00\x00\x00'
print(struct.pack('i',12))
s = struct.pack('i',12)
print(struct.unpack('i',s)[0])
複製程式碼

socket_server_tcp

from socket import *
import subprocess
import struct
# ip_port = ('127.0.0.1',8080)
# back_log=5
# buffer_size = 1024
#
# tcp_server = socket(AF_INET,SOCK_STREAM)
# tcp_server.bind(ip_port)
# tcp_server.listen(back_log)
#
# while True:
#     conn,addr = tcp_server.accept()
#     print('新的客戶端連結',addr)
#     while True:
#         try:
#             cmd=conn.recv(buffer_size)
#             print('收到的客戶端的命令',cmd)
#
#             res = subprocess.Popen(cmd.decode('utf-8'), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
#             err = res.stderr.read()
#             if err:
#                 cmd_res = err
#             else:
#                 cmd_res = res.stdout.read()
#
#             if not cmd_res:
#                 cmd_res = '執行成功'.encode('gbk')
#             #計算資料長度
#             length =len(cmd_res)
#             data_length=str
#             #將資料長度傳送給客戶端
#             conn.send(str(length).encode('utf-8'))
#             #服務端接收到客戶端傳送的準備指令
#             client_ready = conn.recv(buffer_size)
#             #真正的進行傳送資料
#             if client_ready == b'ready':
#                 conn.send(cmd_res)
#         except Exception as e:
#             print(e)
#             break
#------------------------------------------------------
ip_port = ('127.0.0.1',8080)
back_log=5
buffer_size = 1024

tcp_server = socket(AF_INET,SOCK_STREAM)
tcp_server.bind(ip_port)
tcp_server.listen(back_log)

while True:
    conn,addr = tcp_server.accept()
    print('新的客戶端連結',addr)
    while True:
        try:
            cmd=conn.recv(buffer_size)
            print('收到的客戶端的命令',cmd)

            res = subprocess.Popen(cmd.decode('utf-8'), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
            err = res.stderr.read()
            if err:
                cmd_res = err
            else:
                cmd_res = res.stdout.read()

            if not cmd_res:
                cmd_res = '執行成功'.encode('gbk')
            #計算資料長度
            length =len(cmd_res)
            data_length=struct.pack('i',length)
            #此時已知第一次傳送的長度是4個位元組
            conn.send(data_length)
            conn.send(cmd_res)
        except Exception as e:
            print(e)
            break

複製程式碼

socket_client_tcp

from socket import *
import struct
from functools import partial
# ip_port = ('127.0.0.1',8080)
# back_log=5
# buffer_size=1024
#
# tcp_client = socket(AF_INET,SOCK_STREAM)
# tcp_client.connect(ip_port)
#
# while True:
#     cmd = input('>>:').strip()
#     if not cmd:continue
#     if cmd == 'quit':break
#     tcp_client.send(cmd.encode('utf-8'))
#     #解決粘包
#     #客戶端接收到服務端傳送的資料長度
#     length = tcp_client.recv(buffer_size)
#     #客戶端傳送一個自定義的準備操作
#     tcp_client.send(b'ready')
#     length = int(length.decode('utf-8'))
#
#     recv_size = 0
#     recv_msg =b''
#     while recv_size <length:
#         recv_msg += tcp_client.recv(buffer_size)
#         recv_size=len(recv_msg)
#
#     print('命令的執行結果是',recv_msg.decode('gbk'))
# tcp_client.close()
#-----------------------------------------------------------
ip_port = ('127.0.0.1',8080)
back_log=5
buffer_size=1024

tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)

while True:
    cmd = input('>>:').strip()
    if not cmd:continue
    if cmd == 'quit':break
    tcp_client.send(cmd.encode('utf-8'))
    #解決粘包
    length_data = tcp_client.recv(4)
    length = struct.unpack('i',length_data)[0]
    # recv_size = 0
    # recv_msg = b''
    # while recv_size <length:
    #     recv_msg += tcp_client.recv(buffer_size)
    #     recv_size=len(recv_msg)
    recv_msg = ''.join(iter(partial(tcp_client.recv,buffer_size),b''))

    print('命令的執行結果是',recv_msg.decode('gbk'))
tcp_client.close()
複製程式碼

iter補充

l = ['a','b','c','d']
def test():
    return l.pop()
x = iter(test,'b')
print(x.__next__())
print(x.__next__())
print(x.__next__())

from functools import partial

def add(x,y):
    return x+y
print(add(1,2))

func = partial(add,1)
print(func(1))
print(func(2))

複製程式碼

相關文章