Python Network programming

Source: Internet
Author: User

Python provides two levels of access to network services:

    • The low-level network service supports the basic socket, which provides the standard BSD Sockets API to access all the methods of the underlying operating system socket interface.
    • The high-level Network Service module, Socketserver, provides a server-centric class that simplifies the development of network servers.
Socket
Function Describe
S.bind () Bind an address to a socket, and the address in the form of a tuple (host,port) under Af_inet
S.listen () Start TCP snooping. The backlog specifies the maximum number of connections that the operating system can suspend before rejecting the connection. This value is at least 1
S.accept () Passively accepts TCP client connections, (blocking) waits for a connection to arrive
S.connect () Active initialization of TCP server connection, general address format is tuple (hostname,port), if connection error, return socket.error error
S.RECV () Receives TCP data, the data is returned as a string, bufsize specifies the maximum amount of data to receive, and flag provides additional information about the message
S.send ()

Sends the TCP data to the connected socket by sending the data in the string. The return value is the number of bytes to send, which may be less than the byte size of the string

S.sendall () Sends the TCP data in full, sends the data in the string to the connected socket, attempts to send all data before returning, succeeds, returns none, throws an exception if failed
S.recvfrom () The UDP data is received, similar to recv (), but the return value is (data,address). Where data is the string that contains the received information, and address is the socket that sends the data
S.sendto () Send UDP data, send data to a socket, address is a tuple in the form of (Ipaddr,port), specify a remote address, and the return value is the number of bytes sent.
S.close () Close socket
S.getpeername () Returns the remote address of the connection socket, which is typically a tuple (ipaddr,port).
S.getsockname () Returns the socket's own address
S.setsockopt (Level,optname,value) Sets the value of a given socket option
S.getsockopt (Level,optname,[,buflen]) Returns the value of the socket option
S.settimeout (Timeout) Sets the timeout period for the socket operation, and timeout is a floating-point number in seconds. A value of None indicates no over-time. In general, hyper-times should be set when a socket is just created, as they may be used for connected operations
S.gettimeout () Returns the value in seconds for the current timeout period. Returns none if no timeout period is set
S.fineno () Returns the file descriptor of a socket
S.setblocking (flag) If flag is 0, the socket is set to non-blocking mode, otherwise the socket is set to blocking mode (the default value). In nonblocking mode, if the call recv () does not find any data, or the Send () call cannot send the data immediately, the SOCKET.ERROR exception is caused
S.makefile () Create a file associated with the socket
Service-side Example
1 #!/usr/bin/python2 ImportSocket3 ImportSYS4 5 #Create a Server socket object6ServerSocket =Socket.socket (socket.af_inet, socket. SOCK_STREAM)7 8 #get Local Host name9Host =Socket.gethostname ()TenPort = 9999 One #Binding Monitoring A Serversocket.bind ((host,port)) - #set maximum number of connections -Serversocket.listen (5) the  whileTrue: -CLIENTSOCKET,ADDR =serversocket.accept () -msg ='Hello World' -Clientsocket.send (Msg.encode ('Utf-8')) +Clientsocket.close ()
Network module
Agreement Function Port number Module
HTTP Web Access 80 Httplib,urllib,xmlrpclib
NNTP Read and post news articles 119 Nntplib
Ftp File transfer 20 Ftplib,urllib
Smtp Send mail 25 Smtplib
POP3 Receive mail 110 Poplib
IMAP4 Get Mail 143 Imaplib
Telent Command line 23 Telnetlib
Gopher Information Lookup 70 Gopherlib,urllib

Python Network programming

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.