Python socket socket for communication

: First of all: We explain what is socket socket
after 1. socket in the operating system abstraction layer it is in the application layer and the transport layer, which is a set of very simple to operate interface (receiving data), this interface accepts data pay operating systems
then why? directly to the operating system is not more convenient? Well you thought wrong

Because the operating system interface than we thought it would be ugly and complicated, use the operating system to exchange data, non-Shing tedious,, developers can only find a way to broker a deal with them to simple data exchange, then that socket sets its role is to take the word: the operating system and the data exchange between these troublesome operation for encapsulation sophistication, and simplify,
2. we can use it to achieve simple communication
has a simple built-in socket module we Python it can be implemented using a simple communication protocol based on TCP

    #基于切换连接的循环通信
    import socket #首先我们在文件中导入socket 模块
    phone = socket.socket() #实例化一个对象
    phone.bind(('127.0.01',8765))#bind是存放ip地址的(这里存放的是本地的IP地址)
    phone.listen(5)#可以设置连接的个数,
    while 1:#循环等待连接
        conn,adds = phone.accept()
        while 1:#要循环聊天我们要重复接收发送
            try: #异常处理
                receives_commands = conn.recv(1024) #接收命令请求,最多接收1024字节当然你也可以调整
                receivse_commands = receives_commands.decode('utf-8')#网络传输是以字节的形势的所以我们要进行解码
                print(f'来自客户端的消息{receives_commands}')  #打印消息
                if receives_commands.upper(0 == 'Q':break  #正常结束
                dispatch_orders = inport('请输入:').strip().encode('utf-8')
                conn.send(dispatch_orders)
           except ConnectionResetError:#客户端异常结束
                print('客户端终端')
                break
       conn.close() #关闭连接
    phone.close#关闭服务端
    #客户端
    import socket
    phone = socket.socket()
    phone.connect(('127.0.01',8765))        #连接服务端地址            
 while 1:                  
        dispatch_orders = inport('请输入:').strip().encode('utf-8')#发送请求
        if not dispatch_orders:print('不能为空')#不能为空
        phone.send(dispatch_orders)      #发送                        
        if dispatch_orders.upper() ==b'Q':#正常退出
            break
        receives_commands = phone.recv(1024)#接收服务端的回执
        receives_commands = receives_commands.decode('utf-8')#解码
        print(f'来自客户端的消息{receives_commands}')   #打印                                
    phone.close()#关闭客户端
                                           

Then we have implemented a simple communication links circulating through the socket
in which we need to note that:
1. blocking accept and recv when the server and client are in the same kind of obstruction, then who is to accept or synchronous transmission synchronous state so is at rest, this is unreasonable (that bug)
2. when we design we need to know when to send can not be empty, or the server is not receiving messages so we set a judgment
we sometimes find when we accept What more than 1024 bytes will happen?
so here we come to the answer to this question:
Here we introduce an operating system module
module in Python subprocess module is used to communicate and cmd command line

 obj = subprocess.Popen('dir',#实例化对象括号内第一个参数就是我们的cmd命令行的命令,这里我们写的是dir显示文件夹中内容
                                   shell=True,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,

                                   )

            print(obj.stdout.read().decode('gbk'))  # 正确命令
            print(obj.stderr.read().decode('gbk'))  # 错误命令

So we set up a server on this basis in response to a client command mechanism

import socket
import subprocess
phone = socket.socket()
phone.bind(('127.0.01',8695))
phone.listen(5)
while 1:
    conn,adds = phone.accept()
    while 1:
        try:
            receives_commands = conn.recv(1024)
            if receiver_commands ==b'Q':break 
            #obj = subprocess.Popen(Receiving_instructiors,
                                  # shell= True,
                          #stdout = subprocess.PIPE,
                          #stderr = subprocess.PIPE

                                   )
        obj = subprocess.Popen(receives_commands.decode('utf-8')#动态的传入命令
                          shell = True
                          stdout = subprocess.PIPE
                          stderr = subprocess.PIPE 
                          )
  # print(obj.stdout.read().decode('gbk'))  # 正确命令
  # print(obj.stderr.read().decode('gbk'))  # 错误命令
        ret = obj.stodut.read()+obj.stderr.read()#将产生的内容进行拼接
        conn.send(ret)
    except ConnectionResetError:
            print('客户端终端')
            break    
    conn.close()
phone.close()

import socket

phone = socket.socket()

phone.connect(('127.0.01',8695))
while 1:
    dispatch_orders = inport('请输入命令:').strip().encode('utf-8')
    if not dispatch_orders:print('输入不能为空')
    phone.send(dispatch_orders)
    if dispatch_orders.upper() ==b'Q':break
    receives_commands = phone.recv(1024)
    receives_commands = receives_commands.decode('utf-8')
    print(f'来自客户端的消息{receives_commands}')
phone.close()     

Then we will find a problem, when we enter the help command of more than 1024 bytes So how we do
also found that part of the problem is that when we enter the next command, the command did not help out or transfer is complete so is this is a stick pack
so what is causing it?
how to solve it?
next blog to explain

Guess you like

Origin www.cnblogs.com/wuzifan/p/11410805.html