python实现简单的socketserver实例

本文实例讲述了python实现简单的socket server的方法。分享给大家供大家参考。具体如下:

import socket
host = ”
port = 55555
myserversocket = socket.socket(socket.af_inet, socket.sock_stream)
myserversocket.setsockopt(socket.sol_socket, socket.so_reuseaddr,1)
myserversocket.bind((host, port))
myserversocket.listen(1)
print “server is running on port %d; press ctrl-c to terminate.” % port
while 1:
clientsock, clientaddr = myserversocket.accept()
clientfile = clientsock.makefile(‘rw’, 0)
clientfile.write(“welcome, ” + str(clientaddr) + “\n”)
clientfile.write(“please enter a string: “)
line = clientfile.readline().strip()
clientfile.write(“you entered %d characters.\n” % len(line))
clientfile.close()
clientsock.close()

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论