164.html

来自「Python Ebook Python&XML」· HTML 代码 · 共 195 行

HTML
195
字号

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; Networking Concepts</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="161.html" class="navtitle">10. Basic Network Background</a> &gt; <span class="nonavtitle">Networking Concepts</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="163.html" title="Networking"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=164" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="164.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="165.html" title="HTTP"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A41%3A44+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162148045048067056107174087</font><a href="read1.asp?bookname=0672319942&snode=164&now=5%2F31%2F2002+4%3A41%3A44+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
				<h3>Networking Concepts</h3>
				<p>Networking systems are well-defined by the <a NAME="idx1073745634"></a>
					<a naME="idx1073745635"></A>
					<I>OSI/ISO</i> (<i>Open Systems Interconnection/ International Standards Organization</i>) seven-layer model, which suggests the following levels of the networking process: physical, data link, network, transport, session, presentation, and application. However, keep in mind that, in practice, protocols span multiple layers, and you shouldn't worry if your application doesn't fit in this model. Most of today's networking stacks (including TCP/IP) use less layers that are not quite as well separated as in the OSI model. Consequently, if you try to map a TCP/IP session onto the <a NAME="idx1073745636"></a>
					<a name="idx1073745637"></a>
					<a name="idx1073745638"></a>
					<a name="idx1073745639"></a>OSI model, you will get a bit confused because some layers are merged, and some others are removed.</p>

				<BloCkquOte>
<p>
						<p><B>
					
					Physical layer棤</b>
Defines the information necessary to transport data over physical components, such as cables.</p>

					</p>
<P>
						<P><B>
					
					Data link layer棤</B>
Defines how data is passed to and from the physical components. Point-to-point error correction is usually performed at this layer.</p>

					</p>
<p>
						<p><B>
					
					Network layer棤</B>
Organizes the network by assigning distinct addresses for its elements, so the information in traffic can be routed to the right computers. The IP protocol works at this layer.</P>

					</P>
<p>
						<p><b>
					
					Transport layer棤</b>
Packs the data and makes sure that the data transfer between the machines is error-free. TCP and UDP are protocols that implement these responsibilities.</P>

					</P>
<P>
						<P><b>
					
					Session layer棤</b>
Handles each individual connection (session) made by the machines.</p>

					</p>
<P>
						<P><B>
					
					Presentation layer棤</B>
Used to overcome differences such as different formats for integers on different platforms. TCP/IP makes this the application's responsibility, and Python has some modules to help with this (for instance, the <tt class="monofont">struct</tt> module).</p>

					</p>
<p>
						<p><b>
					
					Application layer棤</b>
Implements your final product, your application. FTP clients, SMTP/POP3 mail handlers, and HTTP browsers are examples of complete applications that run over your network.<a name="idx1073745640"></A>
								<a nAme="idx1073745641"></a>
								<A name="idx1073745642"></A>
								<a naME="idx1073745643"></A>
							</P>

					</p>
</bloCKQUote>
				<p>Network connections can be of two types: connection-oriented or connectionless (packet-oriented).</P>

				<P>Let's talk about the pair <A Name="idx1073745644"></a>
					<A NAMe="idx1073745645"></a>
					<a name="idx1073745646"></a>
					<a name="idx1073745647"></a>TCP/IP, which is a packet-oriented implementation. Nowadays, I can't imagine a unique machine that doesn't support it. TCP/IP is the most widely used networking protocol possibly because it is robust and untied to any particular physical medium, and maybe also because the specifications are freely available.</p>

				<p>TCP/IP was originally created by the <a namE="idx1073745648"></a>United States Department of Defense, and soon, this protocol combination became the network of choice for the U.S. government, the Internet, and the universities. This tuple runs on virtually every operating system platform, which makes it strong when internetworking between different LAN environments is required. Today, a great number of commercial and public networks are built on top of this implementation. Although the Internet grew out of the TCP/IP work done at universities and the U.S. Department of Defense, it didn't adopt TCP/IP until part of the way through.</p>

				<P>The network layer of the TCP/IP stack is provided by the <a naMe="idx1073745649"></a>
					<a nAme="idx1073745650"></a>
					<A NAMe="idx1073745651"></a>Internet Protocol (commonly known as IP). This protocol provides the basic mechanism for routing packets in the Internet because it sends packets of data back and forth without building an end-to-end connection.<a nAME="idx1073745652"></A>
					<a namE="idx1073745653"></A>
					<A Name="idx1073745654"></a>
					<A NAMe="idx1073745655"></a>
				</p>

				<p>IP doesn't understand the relationships between packets, and doesn't perform retransmission. (It is not a reliable communication protocol!) Therefore, it requires higher-level protocols such as TCP and UDP to provide a reliable class of service. It does ensure that the IP header is not corrupted though.<a name="idx1073745656"></a>
					<a name="idx1073745657"></a>
					<a namE="idx1073745658"></a>
				</p>

				<P>TCP stands for <i>Transmission Control Protocol,</i> and it is the main form of communication over the Internet because it provides a reliable, session-based service for the delivery of sequenced packets.</p>

				<P>This connection-oriented protocol provides a reliable two-way connection service over a session. Each packet of information exchanged over a session is given a sequence number through which it gets tracked and individually acknowledged. Duplicate packages are detected and discarded by the session services. Sequence numbers are not globally unique or even necessarily unique to the session. Although in a small enough time window, they would be unique to the session.</p>

				<p>The TCP/IP protocol doesn't provide an application interface layer梩he application provides the application layer. However, sockets have emerged as TCP/IP's premier peer-to-peer API, providing a way of writing portable networking applications.<a nAme="idx1073745659"></a>
					<A NAMe="idx1073745660"></a>
					<a nAME="idx1073745661"></A>
				</p>

				<p>
					<a nAME="idx1073745662"></A>
					<a namE="idx1073745663"></A>
					<A Name="idx1073745664"></a>UDP, which stands for <i>User Datagram Protocol,</i> is another protocol that provides transport services. This protocol provides an unreliable but fast datagram service. They are unreliable in the sense that they are not acknowledged or tracked through a sequence number. After transmitting the diagram, you have to hope that it gets received. We don't know if the recipient is there, or even if he is expecting a diagram. Some statistics say that about 5% of the diagrams don't make it. That's depressing, isn't it?</p>

				<div class="note"><p clasS="notetitle"><b>Note</b></P><p>

					<p>UDP is useful for streaming media, where a packet that is late is useless, so retransmission is not desirable.</p>

				</P></div>
<bR>
<br>

				<p>UDP is a connectionless transport protocol that doesn't guarantee delivery or packet sequence. As an example, UDP is used by the <TT CLass="monofont">ping</tT> command in order to check whether a host is reachable in the network.</P>

				<P>No doubt the UDP protocol is faster than the TCP protocol. The reason is because the TCP protocol spends more time switching information between the machines in order to guarantee that the information gets transferred. That doesn't happen when using UDP, which makes it considerably faster than TCP. Another fact is that while transferring data packets, the TCP protocol waits until all the packets arrive, and organizes them in sequence for the client program. However, the UDP protocol doesn't do that. It allows the client program to decide how the packets should be interpreted because packets aren't received in any specific ordering format. The problem is that this kind of implementation is completely unreliable because there is no way to confirm whether the information has reached its destiny. If you need a stream-oriented protocol, TCP is about as fast as you will get it. If it was such a bad protocol, it would have been replaced by now.<A name="idx1073745665"></A>
					<A NAme="idx1073745666"></a>
					<a NAME="idx1073745667"></a>
				</p>

				
					<h4>
				Protocols</h4>
					<p>The most commonly used application protocols are built on top of TCP/IP infrastructures. Actually, they don't have to know any details about TCP nor about IP because a thin layer called <a name="idx1073745668"></a>
						<a name="idx1073745669"></a>
						<tt cLasS="monofont">sockets</tt> exists between TCP/IP and them.<a Name="idx1073745670"></a>
						<A namE="idx1073745671"></A>
						<A Name="idx1073745672"></a>
					</P>

					<P>Python has modules that handle and support the access to all the following protocols. These protocols use the services provided by the sockets in order to transport packets on the network and to make connections to other hosts.</P>

					<Ul>
<li>
							<p>HTTP processes Web pages.</P>

						</LI>
<Li>
							<p>FTP transfers files between different machines.</p>

						</lI>
<LI>
							<P>Gopher browses Gopher servers.</p>

						</li>
<li>
							<p>Telnet provides access to another machine.</p>

						</li>
<li>
							<p>POP3 reads mail files on POP3 servers.</p>

						</li>
<li>
							<p>IMAP reads mail files on IMAP servers.</p>

						</lI>
<li>
							<P>NNTP provides access to the Usenet news.</p>

						</li>
<Li>
							<p>SMTP sends mail to standard mail servers. <a nAme="idx1073745673"></a>
							</P>

						</LI>
</Ul>
				
				
					<h4>
				
				Addresses</h4>
					<p>A <A NAMe="idx1073745674"></a>
						<a nAME="idx1073745675"></A>socket address, on the TCP/IP internet structure, consists of two parts: an Internet address (commonly known as an IP address) and a port number.</p>

					<p>The <a nAME="idx1073745676"></A>
						<a name="idx1073745677"></a>IP address defines the addressing and routing of information around the network, uniquely identifying a network interface.</p>

					<p>An IP address is a 32-bit number (a sequence of four bytes), usually represented by four decimal numbers ranging from 0 to 255, separated by dots. A IP address looks something similar to <tt class="monofont">128.85.15.53.</tt>
					</p>

					<p>Each IP <a NamE="idx1073745678"></a>number must be unique for each TCP/IP network interface card within an administered domain, which in most cases means that each machine connected to the Internet has a unique IP address. Actually, a networked machine can have more Internet addresses than network interfaces. This is quite common in virtual hosting situations.</p>

					<p>A <A name="idx1073745679"></A>port is an entry point to an application/service that resides on a server. It is a number represented by a 16-bit integer. This number can range between 0 and 65535, but you can't freely use all of them inside your programs. Always choose a port number greater than 1024 because the range 0

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?