Import Socket " " Socket.socket (socket_family,socket_type,protocal=0) socket_family can be Af_unix or af_inet. Socket_type can be sock_stream or sock_dgram. Protocol is generally not filled, the default value is 0. Gets the TCP/IP socket tcpsock = socket.socket (socket.af_inet, socket. SOCK_STREAM) Gets the UDP/IP socket udpsock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM) Because there are too many properties in the socket module. We made an exception here by using the ' from module import * ' statement. With the ' from socket import * ', we take all the attributes in the socket module into our namespace, which can greatly reduce our code. For example Tcpsock = socket (af_inet, sock_stream)"
Seven TCP-based sockets
TCP Service Side
# Create a server socket 2 ss.bind () # bind address to Socket 3 Ss.listen () # Listen link 4 inf_loop: # server infinite loop 5 # Accept Client Link 6 comm_loop: # communication Loop 7 # conversation (Receive and send)8 cs.close () # close the client socket 9 ss.close () # Close the server socket (optional)
View Code
TCP Client
1 CS = socket () # Create client Socket 2 cs.connect () # try to connect to server 3 comm_loop: #< /c10> Communication Loop 4 cs.send ()/cs.recv () # conversation (send/Receive)5 cs.close () # Close Client sockets
View Code
The socket communication process is similar to the call process, so we use the phone as an example to implement a low version of socket communication
#_*_coding:utf-8_*_ Service Side__author__='Linhaifeng'ImportSocketip_port=('127.0.0.1', 9000)#Calling cardbufsize=1024#size of sending and receiving messagesS=socket.socket (Socket.af_inet,socket. SOCK_STREAM)#buy a cell phoneS.bind (Ip_port)#Mobile Phone CardS.listen (5)#Phone Standbyconn,addr=s.accept ()#Phone Answering phone#PRINT (conn)#print (addr)Print('receive a call from%s'%addr[0]) msg=CONN.RECV (BUFSIZE)#listen to the news, obey.Print(Msg,type (msg)) Conn.send (Msg.upper ())#send a message, speakconn.close ()#Hang up the phones.close ()#Mobile phone off
#_*_coding:utf-8_*_ Client__author__='Linhaifeng'ImportSocketip_port=('127.0.0.1', 9000) BUFSIZE=1024s=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) s.connect_ex (ip_port)#Dial PhoneS.send ('Linhaifeng NB'. Encode ('Utf-8'))#Send message, Talk (only byte type can be sent)Feedback=S.RECV (BUFSIZE)#receive the message, listen.Print(Feedback.decode ('Utf-8') ) S.close ()#Hang up the phone" "the problem with the above process is that the server can only accept a link, and then completely shut down, the actual situation should be, the service will continue to accept the link, and then loop communication, after the communication is complete only close the link, the servers can continue to receive the next link, the following is the modified version""
#_*_coding:utf-8_*___author__='Linhaifeng'ImportSocketip_port=('127.0.0.1', 8081)#Calling cardbufsize=1024s=socket.socket (Socket.af_inet,socket. SOCK_STREAM)#buy a cell phoneS.bind (Ip_port)#Mobile Phone CardS.listen (5)#Phone Standby whileTrue:#new Receive link loop, can not stop to answer the phoneConn,addr=s.accept ()#Phone Answering phone #PRINT (conn) #print (addr) Print('receive a call from%s'%addr[0]) whileTrue:#new communication cycle, can continue to communicate, send and receive messagesMSG=CONN.RECV (BUFSIZE)#listen to the news, obey. #If Len (msg) = = 0:break #如果不加, then the client being linked suddenly disconnects, recv no longer blocks, and the dead loop occurs Print(Msg,type (msg)) Conn.send (Msg.upper ())#send a message, speakconn.close ()#Hang up the phones.close ()#Mobile phone offservice-side improved versionView Code
#_*_coding:utf-8_*___author__='Linhaifeng'ImportSocketip_port=('127.0.0.1', 8081) BUFSIZE=1024s=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) s.connect_ex (ip_port)#Dial Phone whileTrue:#new communication cycle, the client can constantly send messagesMsg=input ('>>:'). Strip ()ifLen (msg) = = 0:Continues.send (Msg.encode ('Utf-8'))#Send message, Talk (only byte type can be sent)Feedback=S.RECV (BUFSIZE)#receive the message, listen. Print(Feedback.decode ('Utf-8') ) S.close ()#Hang up the phoneClient Improvement EditionView Code
Python (Socket Programming--2)