The socket is a starting point and a focus for two-way communication, the ambience server side and the client
The socket server side uses the method to have
| Method |
Description |
| S.bind () |
This method binds the address (hostname, port number pair) to the socket. |
| S.listen () |
This method sets the up and start TCP listener. |
| S.accept () |
This passively accept TCP client connection, waiting until connection arrives (blocking). |
The method used by the socket client is
| Method |
Description |
| S.connect () |
This method actively initiates TCP server connection. |
The socket general method has
| Method |
Description |
| S.RECV () |
This method receives TCP message |
| S.send () |
This method transmits TCP message |
| S.recvfrom () |
This method receives UDP message |
| S.sendto () |
This method transmits UDP message |
| S.close () |
This method closes socket |
| Socket.gethostname () |
Returns the hostname. |
The following is an example of a simple socket communication, including a socket server to listen to a local server port, a socket client sends a request to the server side. A message is returned after a request is received by the server side
Socket Server Side
Created a simple socket that binds to the port on the local server to provide services externally. The supervisor responds when he hears the request.
S.listen indicates start monitoring, number of listening queue is 5
1 ImportSocket2 3 #Create a socket Object,and bind it to the port4s=Socket.socket ()5Host =Socket.gethostname ()6Port = 123457 S.bind ((host,port))8 9S.listen (5)Ten One whileTrue: AC,ADDR =s.accept () - Print 'Got connectoion from', addr -C.send ('thank from connecting') theC.close ()
Socket Client
Import sockets== = 12345s.connect ((host,port))print s.recv (1024x768) s.close ()
Python Basic-socket Communication