xywuyiba7 发表于 2018-8-7 08:48:12

47. Python socket编程 2

import socket  
import time
  
if __name__ == "__main__":
  
    host = "192.168.0.103"
  
    port = 12345
  
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
    s.connect((host, port))
  
    stat = 1
  
    while 1:
  
      cmd = raw_input("{0} >>> ".format(host))
  
      if cmd.lower() == "exit":
  
            exit()
  
      if not cmd:
  
            continue
  
      s.sendall(cmd)
  
      data = s.recv(2048)
  
      if len(data) > 0:
  
            stat = 1
  
            print ("{0}".format(data))
  
      else:
  
            stat += 1
  
            time.sleep(1)
  
            if stat == 5:
  
                break
页: [1]
查看完整版本: 47. Python socket编程 2