Network programming in Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.