Python -- Network Programming 3

  • Socket socket (module): responsible for receiving data from the transport layer, processing it, and handing it to the operating system from the bottom layer.

    • Effect
      • To avoid the use of interfaces and protocols in each layer of computer learning, socket has encapsulated all interfaces, which is convenient to use and improves development efficiency.
      • socket is essentially a module. By using the function provided by the learning module, the communication between the client and the server is established, which is convenient to use.
  • Socket realizes single communication between a single customer and the server:

    • Server:

      import socket
      
      # 1. Create socket object (buy mobile phone)
      phone = socket.socket() # Can default not to write
      
      # 2. Bind ip address and port (card handling)
      phone.bind(('127.0.0.1', 8848))  # Local loopback address
      
      # 3. Monitoring (power on)
      phone.listen(5)
      
      # 4. Receive connection
      print('start')
      conn, addr = phone.accept()  # The program temporarily enters the standby mode and waits for the client to send information
      print(conn,addr)
      
      from_client_data = conn.recv(1024)  #Receive up to 1024 bytes
      print(f'From client{addr}news{from_client_data.decode("utf-8")}')
      to_client = input('>>>')
      conn.send(to_client.encode('utf-8'))
      
      conn.close()
      phone.close()
    • Client:

      import socket
      
      # Create socket object (buy mobile phone)
      phone = socket.socket() # Can default not to write
      
      # Connecting server ip address and port
      phone.connect(('127.0.0.1', 8848))
      
      # Send news
      to_server = input('>>>').strip()
      phone.send(to_server.encode('utf-8'))
      # receive messages
      from_server_data = phone.recv(1024)  # The program temporarily enters the standby mode and waits for the data from the server to be transmitted.
      
      print(f'From server message:{from_server_data.decode("utf-8")}')
      
      # Shutdown
      phone.close()
  • socket realizes communication cycle:

    • Server:

      import socket
      
      phone = socket.socket()
      
      phone.bind(('127.0.0.1', 8888))
      
      phone.listen(5)
      
      # Receiving connection
      
      while 1:
          print('start')
          conn, addr = phone.accept()  # Program enters standby mode
          print(conn,addr)
          while 1:
              try:
                  from_client_data = conn.recv(1024)  #Receive up to 1024 bytes
                  if from_client_data == b'q':
                      break
                  print(f'From client{addr}news{from_client_data.decode("utf-8")}')
                  to_client = input('>>>')
                  conn.send(to_client.encode('utf-8'))
              except ConnectionResetError:
                  break
          conn.close()
      phone.close()
    • Client:

      import socket
      
      # Create socket object (buy mobile phone)
      phone = socket.socket() 
      
      # Connecting server ip address and port
      phone.connect(('127.0.0.1', 8888))
      
      # Send news
      while 1:
          to_server = input('>>>').strip()
          if to_server.upper() == 'Q':
              phone.send('q'.encode('utf-8'))
              break
          phone.send(to_server.encode('utf-8'))
          # receive messages
          from_server_data = phone.recv(1024)  # Standby, waiting to receive data from the server
          print(f'From server message:{from_server_data.decode("utf-8")}')
      
      # Shutdown
      phone.close()
  • socket realizes communication and connection cycle:

    • Server:

      import socket
      
      phone = socket.socket() 
      
      # 2. Bind ip address and port (card handling)
      phone.bind(('127.0.0.1', 8888))  
      
      # 3. Monitoring (power on)
      phone.listen(5)
      
      # 4. Receive connection
      print('start')
      conn, addr = phone.accept()  # Program tamping
      while 1:
          try:
              from_client_data = conn.recv(1024)  #Receive up to 1024 bytes
              if from_client_data == b'q':
                  break
              print(f'From client{addr}news{from_client_data.decode("utf-8")}')
              to_client = input('>>>')
              conn.send(to_client.encode('utf-8'))
          except ConnectionResetError:
              break
      conn.close()
      phone.close()
    • Client:

      import socket
      
      # 1. Create socket object (buy mobile phone)
      phone = socket.socket() # Can default not to write
      
      # Connecting server ip address and port
      phone.connect(('127.0.0.1', 8888))
      
      # Send news
      while 1:
          to_server = input('>>>').strip()
          if to_server.upper() == 'Q':
              phone.send('q'.encode('utf-8'))
              break
          phone.send(to_server.encode('utf-8'))
          # receive messages
          from_server_data = phone.recv(1024)  # Hold it and wait for the data from the server to come.
          print(f'From server message:{from_server_data.decode("utf-8")}')
      
      # Shutdown
      phone.close()
  • socket to obtain the remote command:

    • Server:

      import socket
      import subprocess
      
      phone = socket.socket()
      
      phone.bind(('127.0.0.1', 8888))
      
      phone.listen(5)
      
      # 4. Receive connection
      print('start')
      conn, addr = phone.accept()
      while 1:
          try:
              cmd = conn.recv(1024) #  dir
              obj = subprocess.Popen(cmd.decode('utf-8'),
                                     shell=True,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE,
                                     )
      
              result = obj.stdout.read() + obj.stderr.read()
              conn.send(result)
          except ConnectionResetError:
              break
      conn.close()
      phone.close()
    • User side:

      import socket
      
      phone = socket.socket()
      
      phone.connect(('127.0.0.1', 8888))
      
      # Send news
      while 1:
          cmd = input('>>>').strip()
          phone.send(cmd.encode('utf-8'))
          # receive messages
          result = phone.recv(1024)  # Hold it and wait for the data from the server to come.
          print(result.decode('gbk'))
      
      # Shutdown
      phone.close()

Keywords: PHP socket Mobile shell

Added by incubi on Wed, 23 Oct 2019 20:54:31 +0300