Beginning Python From Novice to Professional (9) - Socket
来源:互联网 发布:手机广角镜头 知乎 编辑:程序博客网 时间:2024/04/27 02:24
Socket
小型服务器:
#!/usr/bin/env pythonimport sockets = socket.socket()host = socket.gethostname()port = 1234s.bind((host,port))s.listen(5)while True:c,addr = s.accept()print 'Got connection from',addrc.send('Thank you for connecting')c.close()小型客户机:
#!/usr/bin/env pythonimport sockets = socket.socket()host = socket.gethostname()port = 1234s.connect((host,port))print s.recv(1024)运行服务器后运行客户机程序:
服务器打印:
Got connection from ('127.0.1.1', 61625)Got connection from ('127.0.1.1', 61626)Got connection from ('127.0.1.1', 61627)Got connection from ('127.0.1.1', 61628)Got connection from ('127.0.1.1', 61629)Got connection from ('127.0.1.1', 61630)Got connection from ('127.0.1.1', 61631)Got connection from ('127.0.1.1', 61632)Got connection from ('127.0.1.1', 61633)Got connection from ('127.0.1.1', 61634)Got connection from ('127.0.1.1', 61635)客户机打印:
Thank you for connecting
1 0
- Beginning Python From Novice to Professional (9) - Socket
- Beginning Python: From Novice to Professional
- Beginning Python From Novice to Professional (3) - 列表操作
- Beginning Python From Novice to Professional (6) - 函数使用
- Beginning Python From Novice to Professional (7) - 类
- Beginning Python From Novice to Professional (8) - 文件方法
- 《Beginning Python From Novice to Professional》学习笔记一:String
- 《Beginning Python From Novice to Professional》学习笔记二:Sequence
- 《Beginning Python From Novice to Professional》学习笔记三:List
- 《Beginning Python From Novice to Professional》学习笔记四:Tuple
- 《Beginning Python From Novice to Professional》学习笔记六:Dictionary
- 《Beginning Python From Novice to Professional》学习笔记七:Statement
- 《Beginning Python From Novice to Professional》学习笔记八:Abstraction
- 《Beginning Python From Novice to Professional》学习笔记十:Exception
- 《Beginning Python From Novice to Professional》学习笔记十一:__Magic__
- 《Beginning Python From Novice to Professional》学习笔记十二:__Magic__
- Beginning Python From Novice to Professional (2) - 命令行运行Python脚本
- Beginning Ubuntu Linux: From Novice to Professional
- WIN phone 8.1 SDK 中遇到的坑 Hyper-V
- vs2008+Qt+MySql环境搭建及提示"Driver not load"的原因分析
- 常见的6种传值
- 使用CMake构建Ogre工程文件时的一些小细节
- Spark学习: 基础函数功能解读
- Beginning Python From Novice to Professional (9) - Socket
- for循环中scanf字符方法
- fo C to C
- Mysql基本操作
- 白月光
- Eclipse Code Recommenders 2.0发布,支持Crowd
- HDU1873 看病要排队
- 彭丽媛赴澳参加G20峰会拥抱考拉
- mysql数据表