Because of the need to do a monitoring of TCP data content of the small plug-in, learning the internal network of packaged transport software sockets:
First, the introduction of the socket is as follows:
The two programs on the network realize the exchange of data through a two-way communication connection, one end of this connection is called a socket. Establish a network communication connection with at least a pair of port numbers (sockets).The socket is essentially a programming interface (API), a TCP/IP encapsulation, and TCP/IP is also available for programmersdo network development using the interface, this is the socket programming interface;HTTP is a sedan that provides a specific form of encapsulation or display data; the socket is the engine that provides the ability to communicate over the network.second, the socket operation process is as follows:As shown in: Both the TCP client and the TCP server side require the above stepsconfiguration steps for TCP server:STEP1:Set up a Socket objectSk = Socket.socket ()STEP2:Assign a specific address in the address family to the socket Sk.bind ((host,port)) STEP3:Turn on the listening process waiting for the client to link the serverSk.listen (1)STEP4:Accept the client's request, get the IP address of the client and its ports portclnt, addr = sk.accept ()STEP5:After the connection is established, wait for the client's data to be accepteddata = CLNT.RECV (1024x768)STEP6:Answer the client's request, send the corresponding data backclnt.sendall (data)STEP7:Close the Socket objectsk.close ()configuration steps for the TCP client:STEP1:Set up a Socket object S = Socket.socket () STEP2:Connect the client to the corresponding server S.connect ((host, port))
STEP3:Send data to the server after successful connections.sendall (cmd)STEP4:Response data waiting for the serverdata = S.RECV (1024x768)STEP5:Close the Socket objectsk.close ()The program is as follows (host side + client):
#-*-coding:utf-8-*-ImportSocketImportUrllibImportJSONImportsysreload (SYS) sys.setdefaultencoding ('utf-+8') Robot_key='d4490d8bf55442b4871cfecd84defb68'Robot_add='http://www.tuling123.com/openapi/api?key='defCommunicate (info): URL= robot_add+robot_key+'&info='+Info Response=urllib.urlopen (URL). Read () Data=json.loads (response)returnU'Robot:'. Decode ('Utf-8') +data['text']host='192.168.1.120'PrintHostport= 5000Sk=Socket.socket () Sk.bind ((host, Port)) Sk.listen (1) whiletrue:clnt, addr=sk.accept ()Print 'Address is:', addr whileTrue:data= CLNT.RECV (1024) if notdata:clnt, addr=sk.accept ()Print 'going to:', data result=Communicate (data)ifLen (Result) = =0:result='EXD'Clnt.sendall (Result) sk.close ()
Client:
#-*-uft-8-*-ImportSocketS=Socket.socket () host='192.168.1.120'Port= 5000S.connect ((host, port)) whileTrue:cmd= Raw_input ('Please enter your cmd:') ifcmd = ="': Continue Print 'Sending command!'s.sendall (cmd) data= S.RECV (1024) Printdatas.close ()
Three, the detailed basic API function:
Socket () function bind () function listen (), connect () function accept () function (read (), write () function close () function
Python Socket Programming