码迷,mamicode.com
首页 > 编程语言 > 详细

Beginning Python From Novice to Professional (9) - Socket

时间:2014-11-15 18:56:56      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:python

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

标签:python

原文地址:http://blog.csdn.net/wu20093346/article/details/41147647

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!