17 Network Client Programming-"Python core programming"

Source: Internet
Author: User
Tags imap nntp remote ftp server ftp client file transfer protocol ftp protocol mail exchange rsync

?? Introduction?? File Transfer?? File Transfer Protocol (FTP)?? network news, Usenet, and newsgroups?? Network News Transfer Protocol (NNTP)?? e-Mail?? Simple Mail Transfer Protocol (SMTP)?? Post Office Protocol 3 (POP3)?? related modules17.1 What is an Internet client?
    Three Internet Protocol--ftp, NNTP and pop3    client/server model 17.2 file Transfer File transfer Internet Protocol     The most popular thing in the Internet is the exchange of files. File Exchange is everywhere.     File Transfer Protocol (FTP)     Unix-to-unix replication Protocol (UUCP)     Web Hypertext Transfer Protocol (HTTP)     Unix remote file replication refers to Make RCP, as well as more secure, more flexible SCP and rsync    HTTP are primarily used for Web page file downloads and access to Web services.     HTTP generally does not require users to enter a login password to access files and services on the server.     HTTP File transfer requests are primarily used to obtain Web pages (file downloads).      SCP and rsync require users to log on to the server, otherwise they cannot upload or download files.     FTP, like Scp/rsync, can upload or download files, but also uses the UNIX multi-user concept, users must enter a valid user name and password to use. However, FTP also allows anonymous logons. The File Transfer Protocol (FTP)     File Transfer Protocol was developed by the late Jon Postel and Joyce Reynolds and was recorded in the RFC (Request for Comment) No. 959 document, which was released in October 1985.     File Transfer Protocol is mainly used to download public files anonymously. It can also be used to transfer files between two computers, especially when using a UNIX system as a file storage system and using other machines to work.     FTP requires a user name and password to access the remote FTP server, but it also allows users without an account to log on as anonymous users.     The commands that anonymous users can use via the FTP protocol are more restrictive than the average user.         on the ground floor, FTP uses only tcp--it does not use UDP.     FTP clients and servers use two sockets to communicate: one is the control and command Port (Port 21st) and the other is the data port (sometimes port 20th).  &nbSp           Figure 17-1 FTP client and server on the Internet . The client and the server send the FTP protocol using the instruction and control ports, and the data is transmitted over the data port. There are two modes of FTP: active and passive. Data ports are used only in active mode servers.        After the server sets port 20th as the data port, it "proactively" connects to the client's data port. In passive mode, the server simply tells the client the number of its random port, and the client must actively establish the data connection. In this mode, you will see that the FTP server is "passive" when establishing a data connection. Python and FTP When using Python's FTP support, all you need to do is import the Ftplib module and instantiate a ftplib.    The FTP class object. All FTP operations (such as login, transfer files, logout, etc.) are done using this object.From ftplib import FTPf = FTP (' ftp.python.org ')f.login (' Anonymous ', ' [email protected] ')    :f.quit ()Ftplib. FTP class Method Interactive FTP sample client FTP program example
1 #!/usr/bin/env python2 3 ImportFtplib4 ImportOS5 ImportSocket6 7HOST ='ftp.mozilla.org'8Dirn ='Pub/mozilla.org/webtools'9FILE ='bugzilla-latest.tar.gz'Ten  One defMain (): A     Try: -f =Ftplib. FTP (HOST) -     except(Socket.error, Socket.gaierror), E: the         Print 'error:cannot Reach "%s"'%HOST -         return -     Print '* * * Connected to host '%s ''%HOST -  +     Try: - F.login () +     exceptFtplib.error_perm: A         Print 'Error:cannnot Login anonymously' at f.quit () -         return -     Print '* * * logged in as "anonymous"' -  -     Try: - f.cwd (Dirn) in     exceptFtplib.error_perm: -         Print 'error:cannot CD to "%s"'%Dirn to f.quit () +         return -     Print '* * * Changed to '%s ' folder'%Dirn the  *     Try: $F.retrbinary ('RETR%s'%FILE,Panax NotoginsengOpen (FILE,'WB'). Write) -     exceptFtplib.error_perm: the         Print 'error:cannot Read File "%s"'%FILE + Os.unlink (FILE) A     Else: the         Print '* * * downloaded "%s" to CWD'%FILE + f.quit () -         return $  $ if __name__=='__main__': -Main ()
Other aspects of FTP Python support both active and passive modes.    Note that in Python2.0 and previous versions, passive mode support is turned off by default, and is turned on by default in Python2.1 and later versions. 17.3 Network News Usenet and newsgroup Usenet news system is a global archive of the "bulletin board."
The Usenet News system is a global archive of "electronic bulletin boards". A variety of topics ranging from poetry to politics, from natural language learning to computer language, software to hardware, planting to cooking, hiring, recruiting, music, magic, breakup, courtship and more. Newsgroups can be global in generalities or only for a geographic region. The entire system is a massive global network of computers, sharing posts on Usenet among computers. If a user posts a post to a local Usenet computer, the post is propagated to other connected computers, These computers are then sent to the computer connected to them until the post is spread all over the world and everyone receives this post. Each system has a list of newsgroups that it has subscribed to, and it only receives posts from newsgroups that it is interested in-not all newsgroups on the server. Usenet newsgroup services depend on the service provider, many of which are accessible to the public, and some are only allowed to be used by certain users, such as paid users, students of specific universities, etc. If a Usenet administrator is set up, you may be asked to enter a user name and password. Administrators can also set whether to allow only uploads or only allow downloads.
Network News Transfer Protocol (NNTP) is called a Network News Transfer Protocol (NNTP) for users to download or post in newsgroups. It was created and documented in RFC 977 by Brain Kantor (University of California, San Diego) and Phil Lapsley (University of California, Berkeley), published in February 1986.        Subsequent updates were recorded in RFC 2980, which was published in October 2000. FTP requires different ports for login, data transfer and control, and NNTP uses only one standard port 119来 for communication. Figure 17-2 NNTP client and server on the Internet . Clients mainly read news and sometimes post. The article synchronizes between servers. As with FTP for Python and NNTP, all we have to do is import the Nntplib Python module and instantiate the class Nntplib.    NNTP and calls the appropriate method.    In general, after you have logged in, you will need to call the group () method to select a newsgroup of interest. The group () method returns the server's return information, the number of articles, the ID of the first and last article, and the name of the group.From nntplib import NNTPn = NNTP (' your.nntp.server ')r,c,f,l,g = N.group (' Comp.lang.python ')        ...n.quit ()Nntplib.    The NNTP class method 17.4 e-mail messages are made up of the header domain (together called the message headers) and later optional message bodies. The RFC stipulates that the message body is optional and only the message header is necessary.
e-mail system components and Protocol message transfer Agent (MTA) MX (mail exchange mail for Exchange)
Send an e-mail to be able to send e-mail, your mail client must be connected to an MTA, they communicate by some kind of protocol. The protocol used for communication between MTA is called a message transfer system (MTS). Only two MTA use this protocol to communicate. As we said at the beginning of this section, because there were many different computer systems, each system used different network software, this kind of communication is dangerous and unpredictable. What's more, some computers use interconnected networks, while others use modems to dial, and the time to send messages is unpredictable.    In fact, I once had an email sent 9 months after the receipt! How can the Internet speed be so slow? Due to these complexities, one of the basics of modern e-mail, the Simple Email Transfer Protocol (SMTP) came into being in 1982. SMTP SMTP was created by the late Jonathan Postel (University of California Information Institute), which was recorded in RFC 821 and published in August 1982. SMTP is the most commonly used MTS for message exchange between the MTA on the Internet. It is used by the MTA to transfer e-mail from one host to another host. When you e-mail, you have to connect to an external SMTP server, your mail program is an SMTP client.                Your SMTP server has become the first springboard for your message. Python and SMTP Yes, there is also a smtplib module and a smtplib.    The SMTP class is to be instantiated. As with NNTP, SMTP Communication, as long as one port。 Like NNTP, logins are optional and only log on when the server has SMTP authentication (Smtp-auth) turned on.From smtplib import SMTPn = SMTP (' smtp.yourdomain.com ')        ...n.quit ()Smtplib.    SMTP class methods for most e-mail senders, only two methods are required: SendMail () and Quit ().    All parameters of SendMail () should follow RFC 2822, that is, the e-mail address must have the correct format, the body of the message should have the correct leading head, followed by two carriage return and line break (\ r \ n) pairs. Note that the actual message body is not necessary.    According to RFC 2822, "The only required header information is the sending date and the sending address", i.e. "date:" and "From:": (mail from, RCPT to, DATA) receive e-mail applications running on home computers are called Mail user agents (MUA).    MUA download messages from the server, they may be deleted automatically during the process (or they may not be deleted, left on the server, and manually deleted by the user). MUA must also be able to send e-mail, which can communicate with the MTA directly with SMTP when sending the message. POP and IMAP
The first protocol for downloading messages is called the Post Office Protocol (POP), which is recorded in RFC 918 and published in October 1984. The purpose of the Post Office Protocol (POP) is to allow the user's workstation to access mail in the mailbox server. Messages to be sent from the workstation to the mail server through the Simple Mail Transfer Protocol (SMTP) "In the years after the pop, there was another protocol called Interactive Mail Access Protocol (IMAP) in today's world, Microsoft Exchange, which occupies most of the mail server market, uses IMAP as a The download mechanism for it. Figure 17-3 e-mail senders and recipients on the Internet . The client communicates with their MUA and the appropriate MTA to download and send the message. E-mail "jumps" from one MTA to another MTA until it reaches its destination. Python and POP3 import Poplib, instantiating Poplib.        POP3 class from poplib import POP3 p = POP3 (' Pop.python.is.cool ') p.user (...)        P.pass_ (...) ... p.quit () poplib. POP3 class method 17.5 related modules

17 Network Client Programming-"Python core programming"

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.