简单的python服务器程序

一个接受telnet输入的服务器端小程序

#!/usr/local/bin/python3.5
#coding:utf-8
import socket

host = ''
port = 51423

s = socket.socket(socket.AF_INET,  socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)

print('Server is running on port %d; press Ctrl-c to terminate.' % port)

while 1:
    clientsock, clientaddr = s.accept()
    clientfile = clientsock.makefile('rwb', 0)
    clientfile.write(('welcome, ' + str(clientaddr) + '
').encode(encoding='utf-8'))
    clientfile.write(('please enter a string: ').encode(encoding='utf-8'))
    line = clientfile.readline().strip()
    clientfile.write(('U are enterd %d characters.
' % len(line)).encode(encoding='utf-8'
))
    clientfile.close()
    clientsock.close()

 效果图

END!

原文地址:https://www.cnblogs.com/changbo/p/5653315.html