Sockets
(Each book has a different version, here is the understanding of Baidu Entry): The source IP address and destination IP address and the combination of source port number and destination port number is called socket. The servers and services that are used to identify client requests. There are two types of sockets that are file-based (UNIX sockets, whose "family name" is Af_unix) and network-based (whose "family name" is Af_inet).
Python only supports Af_unix,af_inet,af_netlink (a Linux socket).
The most common method of socket objects
Types of Sockets
There are only two types of sockets : connection-oriented sockets and non-connected sockets.
connection-oriented sockets
That is, before the communication must establish a connection, it provides a reliable sequence of data transmission, the main protocol to achieve this connection is the Transmission Control Protocol (TCP), to create such a socket needs to specify the socket type Sock_stream, Because such sockets use Internet Protocol (IP) to find hosts in the network, they are generally described using the TCO/IP combination.
Create a TCP Client
From socket import *from time import ctime# set hostname and port host= "" port=21567# Set buffer size bufsiz=1024addr= (host,port) # Create a server socket Tcpsersock=socket (af_inet,sock_stream) #把地址绑定到套接字上面tcpSerSock. bind (ADDR) #最多允许5个连接tcpSerSock. Listen (10) # Server infinite Loop while True: print ("Waiting for Connection ...") #接收客户端连接, the Accept () function makes a blocking Tcpclisock, addr= Tcpsersock.accept () print (' ... connected from: ', addr) #通信循环 while True: data=tcpclisock.recv (bufsiz ) #对话, receive if not data: break Data=data.decode () tcpclisock.send (' [%s]%s '% (CTime (), data)]. Encode () ) Tcpclisock.close () tcpsersock.close ()
For Decode (), encode () problems with sockets, see http://blog.csdn.net/chuanchuan608/article/details/17915959
Create a TCP Client
From socket import * #创建一个TCP客户端, the program prompts the user to enter information to be passed to the server # displays the timestamp result returned by the server host= ' Port=21567bufsiz=1024addr= (host,port) # Create a client socket Tcpclisock=socket (af_inet,sock_stream) #尝试连接服务器tcpCliSock. Connect (ADDR) #与服务器对话while True: data=input (' > ') If not data: #用户没有输入时退出 break tcpclisock.send (data) Data=tcpclisock.rec (bufsiz) if not data:# The server exits when it fails to accept failure for some reason, exit break Print (data) tcpclisock.close ()
non- connected sockets
This means that there is no need to connect to communicate, but the order and reliability of data arrival can not be guaranteed, the main protocol to achieve this connection is the User Datagram Protocol (UDP), to create UDP to be created at the time of the creation of the specified Sock_dgram, ibid, generally will have UDP/IP combination name to describe.
Create a UDP server
From socket import*from time import ctimehost= "port=21567bufsize=1024addr= (host,port) udpsersock=socket (Af_inet, SOCK_DGRAM) Udpsersock.bind (ADDR) while True:print ("Waiting for Message ...") Data,addr=udpsersock.recvfrom ( BUFSIZE) Udpsersock.sendto ((' [%s]%s '% (CTime (), data). Encode (), addr) print (' ... received from and returned to: ', Addr) Udpsersock.close ()
Create a UDP client
From socket import * #不需要和服务器建立链接直接把消息发出去然后等待服务器的回复. host= ' <span style= "font-family:arial, song body, Sans-serif;" > </span> ' port=21567bufsize=1024addr= (host,port) Udpclisock=socket (Af_inet,sock_dgram) while True: Data=input (' > ') if not data: break Data=data.encode () udpclisock.sendto (data,addr) data, Addr=udpclisock.recvfrom (BUFSIZE) if not data: break print (data) udpclisock.close ()
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python Network programming