Python之socket编程浅谈

来源:互联网 发布:linux 查看服务器状态 编辑:程序博客网 时间:2024/05/17 09:36
以下是维基百科的解释:
A network socket is an internal endpoint for sending or receiving data at a single node in a computer network. Concretely, it is a representation of this endpoint in networking software (protocol stack), such as an entry in a table (listing communication protocol, destination, status, etc.), and is a form of system resource.

个人总结(错了另刂扌丁我):
简单说,socket就是通讯链中的句柄;通俗说,就是资源标识符;再俗点,就是手机号。

可以把socket看成为`特殊文件`,该文件就是`服务端`和`客户端`。
socket的编程就是`对文件的操作`
文件的操作:打开--处理(读写)--关闭
所以:
socket的操作:
注:`处理`之前的可以看成为`打开文件`
server端:实例化--bind--listen--处理(accept为阻塞)--close
client端:实例化--connect--处理(recv为阻塞)--close



=================================================================

eg:(Python27)

# server端:import socketsk = socket.socket()sk.bind(('127.0.0.1', 8080))sk.listen(5)while True:conn, address = sk.accept()# ...sk.close()# -------------------------------#client端:import socketobj = socket.socket()obj.connect(('127.0.0.1', 8080))while True:obj.recv(1024)# ...obj.close()


原创粉丝点击