python socket Common Interface Description

First, the server

1. Create a socket object

  socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)

  AF_INET: IPv4 protocol

  SOCK_STREAM: byte stream socket

  Return Value: non-negative integer, referred to as a character representation socket

 2, socket bind address

  socket.bind((host,port))

  To a local protocol address assigned to a socket, this address is not already bound to the other socket.

3, monitor

  socket.listen(5)

  Such socket accepts connections. 5 to set the maximum number of connections, this value can set their own, the minimum value is 0

4, accept the connection

  conn,addr=socket.accept()

  To accept a connection, socket must have an address binding, and is listening for connections.

The return value is (conn, address), where conn is a new socket object, for transmitting and receiving data in this connection. binding address is the address of the other end of the socket connector.

5, the received data

  conn.recv(bufsize)

  Receiving data from the socket, the return value of the received data, the data type is bytes object. Disposable maximum amount of data it receives into bufsize. In the network or hardware is not good enough, the received data may be less than the amount of bufsize.

6, the transmission data

  conn.send(bytes)

   It is connected to send data to a remote socket address. The return value is the number of bytes sent. The return value may be less than the number of bytes to be transmitted. The application is responsible for sending all the data out, if some data is not sent, the application needs to send out to the rest of the data again.

  conn.sendall(bytes)

  The entire interface is responsible for sending data to a remote socket address. If successful return None, failure exception is thrown.

Second, the client

1. Create a socket object

  socket.socket (socket.AF_INET.socket.SOCK_STEAM)

  Create a socket object with the same server

2、socket.connect((host,port))

  Connect to a remote socket address by

3, the received data

  socket.recv()

  The same server recv ()

4, the transmission data

  socket.send()

  The same server send ()

  socket.sendall()

  With server-side sendall ()

Guess you like

Origin www.cnblogs.com/briskzou/p/12080378.html