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

转载于:https://www.cnblogs.com/jzssuanfa/p/6909367.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值