Beginning Python From Novice to Professional (9) - Socket
2014-11-15 17:24
387 查看
Socket
小型服务器:
服务器打印:
小型服务器:
#!/usr/bin/env python import socket s = socket.socket() host = socket.gethostname() port = 1234 s.bind((host,port)) s.listen(5) while True: c,addr = s.accept() print 'Got connection from',addr c.send('Thank you for connecting') c.close()小型客户机:
#!/usr/bin/env python import socket s = socket.socket() host = socket.gethostname() port = 1234 s.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
相关文章推荐
- Beginning Python From Novice to Professional (9) - Socket
- 《Beginning Python From Novice to Professional》学习笔记三:List
- 《Beginning Python From Novice to Professional》学习笔记八:Abstraction
- Beginning Python From Novice to Professional (4) - 演示样本格式字符串
- Beginning Python: From Novice to Professional
- 《Beginning Python From Novice to Professional》学习笔记四:Tuple
- Beginning Python From Novice to Professional (8) - 文件方法
- Beginning Python From Novice to Professional (5) - 条件与循环
- 《Beginning Python From Novice to Professional》学习笔记二:Sequence
- 《Beginning Python From Novice to Professional》学习笔记六:Dictionary
- Beginning Python From Novice To Professional读书笔记
- 《Beginning Python From Novice to Professional》学习笔记十二:__Magic__
- Beginning Python From Novice to Professional (3) - 列表操作
- 《Beginning Python From Novice to Professional》学习笔记一:String
- 《Beginning Python From Novice to Professional》学习笔记七:Statement
- Beginning Python From Novice to Professional (2) - 命令行运行Python脚本
- Beginning Python From Novice to Professional (5) - 条件与循环
- Beginning Python From Novice to Professional (7) - 类别
- Beginning Python From Novice to Professional (4) - 字符串格式示例
- Beginning Python From Novice to Professional (6) - 函数使用