7.3. socket — Low-level networking interface

This module provides access to the BSD socket interface. The socket() function returns a socket object whose methods implement the various socket system calls.

socket.socket(family, type)
Create a new socket using the given address family and socket type.
  • The address family should be socket.AF_INET.
  • The socket type should be socket.SOCK_STREAM or SOCK_DGRAM

7.3.1. Class socket

socket.connect(address)

Connect to a remote socket at address. The format of address is: (ipv4 address, port)

socket.bind(address)

Bind the socket to address. The socket must not already be bound. The format of address is: (ipv4 address, port)

socket.listen(backlog)

Listen for connections made to the socket. The backlog argument specifies the maximum number of queued connections and should be at least 1.

socket.accept()

Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection. The format of address is: (ipv4 address, port)

socket.send(bytes)

Send data to the socket. The socket must be connected to a remote socket.

socket.sendto(bytes, address)

Send data to the socket. The socket should not be connected to a remote socket, since the destination socket is specified by address. Return the number of bytes sent. The format of address is: (ipv4 address, port)

socket.recv(bufsize)

Receive data from the socket. The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by bufsize.

socket.recvfrom(bufsize)

Receive data from the socket. The return value is a pair (bytes, address) where bytes is a bytes object representing the data received and address is the address of the socket sending the data.

socket.setsockopt(level, optname, value)

Set the value of the given socket option. The needed symbolic constants are defined in the socket module (SO_* etc.). The value can be an integer or a bytes-like object representing a buffer.

socket.getsockopt(level, optname)

Get the value of the given socket option. The needed symbolic constants are defined in the socket module (SO_* etc.). The return value can be an integer or a bytes-like object representing a buffer.

socket.close()

Close the socket. Once that happens, all future operations on the socket object will fail. The remote end will receive no more data (after queued data is flushed).

Constants

  • Address family
    • socket.AF_INET
  • Socket types
    • socket.SOCK_STREAM
    • socket.SOCK_DGRAM
  • Socket level
    • socket.SOL_SOCKET
  • Socket options
    • socket.SO_BROADCAST
    • socket.SO_RCVTIMEOUT
    • socket.SO_ERROR

7.3.2. Examples

import socket

# Echo server program
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 5005               # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()

print ('Connected with ' + addr[0] + ':' + str(addr[1]))

while True:
    data = conn.recv(256)
    if not data: break
    conn.send(data)

conn.close()
s.close()
# Echo client program
import socket
import time

HOST = '192.168.2.36'
PORT = 5005

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect( (HOST,PORT) )
s.send('hello')
data = s.recv(256)
print('Received', repr(data))
s.close()

while True:
      time.sleep(5)