Socket () function:
Use the Socket.socket () function to create a socket:
Socket (socket_family,socket_type,protocol=0)
Socket_familu can be af_inet or af_unix.socket_type can be sock_stream (connection-oriented) or SOCK_DGRAM (for non-connected).
For example, create a TCP/IP socket:
Tcpsock=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
To create a UDP/IP socket:
Udpsock=socket.socket (Socket.af_inet,socket. SOCK_DGRAM)
Socket Object methods:
server-side socket function |
|
s.bind () |
bind address (host, Port number) to socket |
s.listen () |
start TCP Listener |
s.accept () |
Passive reception of TCP client connections, (blocking) waiting for a connection to arrive |
client socket function |
|
s.connect () |
active initialization T CP Server Connection |
s.connect_ex () |
connect () function, which returns an error code when an error occurs instead of throwing an exception |
socket function for public use |
description |
s.recv () |
receive TCP data |
s.send () |
send TCP data |
s.sendall () |
full send TCP Data |
s.recvfrom () |
receive UDP data |
s.sendto () |
send UDP data |
socket function for public use |
description |
s.getpeername () |
The address of the remote end of the current socket is connected to |
s.getsockname () |
The address of the current socket |
s.getsockopt () |
Returns the parameter of the specified socket |
s.setsockopt () |
Set the parameters of the specified socket |
s.close () |
close socket |
|
|
s.setblocking () |
Set the blocking and non-blocking mode for sockets (when True is blocked, false is non-blocking) |
s.settimeout () |
|
s.gettimeout () |
timeout to get blocked socket operation |
Functions for file-oriented sockets |
Describe |
S.fileno () |
File descriptor for sockets |
S.makefile () |
Create a file associated with the socket |
Network programming in Python