[Python] python编写的简单的socket server →→→→→进入此内容的聊天室

来自 , 2019-03-11, 写在 Python, 查看 161 次.
URL http://www.code666.cn/view/00720238
  1. import socket
  2.  
  3. host = ''
  4. port = 55555
  5.  
  6. myServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7. myServerSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  8. myServerSocket.bind((host, port))
  9. myServerSocket.listen(1)
  10.  
  11. print "Server is running on port %d; press Ctrl-C to terminate." % port
  12.  
  13. while 1:
  14.     clientsock, clientaddr = myServerSocket.accept()
  15.     clientfile = clientsock.makefile('rw', 0)
  16.     clientfile.write("Welcome, " + str(clientaddr) + "\n")
  17.     clientfile.write("Please enter a string: ")
  18.     line = clientfile.readline().strip()
  19.     clientfile.write("You entered %d characters.\n" % len(line))
  20.     clientfile.close()
  21.     clientsock.close()
  22.  
  23. #//python/8079

回复 "python编写的简单的socket server"

这儿你可以回复上面这条便签

captcha