当前位置:编程学习 > 网站相关 >>

Python写的服务器代码区区20行左右

目前正在学Python网络编程基础,发觉Python是一个不错的脚步,Python写的服务器代码区区20行左右,如果是C或者C++写的话,远不止这个数了。
[python]  www.zzzyk.com
#!usr/bin/python 
 
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('rw',0) 
    clientfile.write("welcome ,"+str(clientaddr)+"\n" ) 
    clientfile.write("Please enter a string:") 
    clientfile.write("\n") 
    line=clientfile.readline().strip() 
    clientfile.write("You entered string is:%s .\n"%line) 
    clientfile.close() 
    clientsock.close() 
运行该程序,然后用客户端连接

\


摘自 loveheronly的专栏
补充:Web开发 , Python ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,