The Python Network Programming (ii) the Socket Socket

Socket socket

  1. Why learn socket must first learn Internet Protocol?

    • First, C / S architecture is network-based communication

      Then the core network that is a bunch of network protocol, which is the standard protocol. If you want to develop a software-based network communication, you must follow these standards

  2. socket layer: socket is between the application layer and the transport layer, the interface provides a simple interface between the operating system and avoiding, eliminating the cumbersome and complex operations .socket rather belong to a module in python provides these to us. simple function.

  1. What socket that?

    • The socket is an application layer and the TCP / IP communication protocol middleware abstraction layer, which is a set of interfaces. In design mode, Socket is actually a facade pattern, it is the complexity of TCP / IP protocol suite is hidden behind the Socket interface for users, a simple interface is all set, let Socket to organize data in order to comply with the specified protocol.

      So, we do not need in-depth understanding of tcp / udp protocol, socket has a good package for us, we just need to follow the provisions of the socket to program, write a program that follows the natural tcp / udp standards.

  2. Socket TCP-based protocol

    • Sockets Category:
      • Based on the file type of socket Family: AF_UNIX (on Unix systems, everything is a file, the file socket calls is based on the underlying file system access to data, two sockets processes running simultaneously on the same machine, you can indirect communication completed by accessing the same file system)
      • Based on the type of network sockets family: AF_INET (python supports a variety of address family, but because we only care about network programming, so most of the time we only use AF_INET)
    • Socket workflow:
      • Example: If you want to give your friend a call, first dial telephone brought a friend after hearing the phone ring, then you and your friends established a connection, you can speak. And other end of the exchange, hang up the phone to end this conversation. Life scenarios to explain this works.
  3. Socket Functions

    • Server socket function:

       import socket
       phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
       # 1.服务端套接字函数
       phone.bind('主机ip地址',端口号)  #绑定到(主机,端口号)套接字
       phone.listen() #开始TCP监听
       phone.accept() #被动接受TCP客户的连接,等待连接的到来
    • The client socket functions:

       import socket
       phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#买手机
       phone.connect()  #主动连接服务端的ip和端口
       phone.connect_ex()  #connect()函数的扩展版本,出错的时候返回错码,而不是抛出异常
    • Server and client public use of the word nested function:

       phone.recv() #接受TCP数据
       phone.send() #发送TCP数据
       phone.recvfrom() #接受UDP数据
       phone.sendto() #发送UDP数据
       phone.getpeername() #接收到当前套接字远端的地址
       phone.getsockname() #返回指定套接字的参数
       phone.setsockopt() #设置指定套接字的参数
       phone.close() #关闭套接字
    • server:

      import socket
      
      # 买电话
      phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  # 默认基于TCP协议的socket
      
      # 绑定电话卡
      phone.bind(('192.168.14.198',8848))  # 绑定IP地址和端口
      
      # 开机监听
      phone.listen(5)
      print(111)
      
      # 等待连接
      conn, addr = phone.accept() # 阻塞
      print(conn,addr)
      
      from_client_data = conn.recv(1024)  # 至多接受1024个字节  阻塞
      print(f'来自客户端{addr[0]}的消息:{from_client_data.decode("utf-8")}')
      
      to_client_data = input('>>>')
      conn.send(to_client_data.encode('utf-8'))
      conn.close()
      phone.close()
    • client

      import socket
      
      # 买电话
      phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  # 默认基于TCP协议的socket
      
      # 拨号打电话
      phone.connect(('127.0.0.1',8848))
      data = input('请输入>>>')
      
      phone.send(data.encode('utf-8'))
      from_server_data = phone.recv(1024)
      print(f'来自服务端的消息:{from_server_data}')
      
      
      # 关闭电话
      phone.close()

Guess you like

Origin www.cnblogs.com/zhangdadayou/p/11431795.html