Beginning Python From Novice to Professional - Socket

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Beginning Python From Novice to Professional - 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 - Socket的主要内容,如果未能解决你的问题,请参考以下文章

start from the beginning-JAVA(接口多态)

start from the beginning-JAVA(成员变量与局部变量的区别封装构造方法)

Beginning Python Chapter 3 Notes

kafka-python 消费者未收到消息

Rails:在时区获取#beginning_of_day

动态规划 is beginning。。。。。。。。。