首页 > 代码库 > Beginning Python From Novice to Professional (9) - Socket
Beginning Python From Novice to Professional (9) - Socket
Socket
小型server:
#!/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)执行server后执行客户机程序:
server打印:
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。