Python之socket编程浅谈

"""

<axiner>声明:
(错了另刂扌丁我)
(如若有误,请记得指出哟,谢谢了!!!)

"""

以下是维基百科的解释:
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 socket  
  
sk = socket.socket()  
sk.bind(('127.0.0.1', 8080))  
sk.listen(5)  
  
while True:  
    conn, address = sk.accept()  
    # ...  
  
sk.close()  
  
# -------------------------------  
#client端:  
import socket  
  
obj = socket.socket()  
obj.connect(('127.0.0.1', 8080))  
  
while True:  
    obj.recv(1024)  
    # ...  
  
obj.close() 

猜你喜欢

转载自blog.csdn.net/atpuxiner/article/details/79387455