PageRenderTime 75ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/PyMOTW/socket/addressing.rst

https://bitbucket.org/dhellmann/pymotw/
ReStructuredText | 442 lines | 319 code | 123 blank | 0 comment | 0 complexity | 2fa92cf82cbe8b522ae8ec029630a283 MD5 | raw file
  1. ================================================
  2. Addressing, Protocol Families and Socket Types
  3. ================================================
  4. A *socket* is one endpoint of a communication channel used by programs
  5. to pass data back and forth locally or across the Internet. Sockets
  6. have two primary properties controlling the way they send data: the
  7. *address family* controls the OSI network layer protocol used and the
  8. *socket type* controls the transport layer protocol.
  9. Python supports three address families. The most common,
  10. :const:`AF_INET`, is used for IPv4 Internet addressing. IPv4
  11. addresses are made up of four octal values separated by dots (e.g.,
  12. ``10.1.1.5`` and ``127.0.0.1``). These values are more commonly
  13. referred to as "IP addresses." Almost all Internet networking is done
  14. using IP version 4 at this time.
  15. :const:`AF_INET6` is used for IPv6 Internet addressing. IPv6 is the
  16. "next generation" version of the Internet protocol, and supports
  17. 128-bit addresses, traffic shaping, and routing features not available
  18. under IPv4. Adoption of IPv6 is still limited, but continues to grow.
  19. :const:`AF_UNIX` is the address family for Unix Domain Sockets (UDS),
  20. an interprocess communication protocol available on POSIX-compliant
  21. systems. The implementation of UDS typically allows the operating
  22. system to pass data directly from process to process, without going
  23. through the network stack. This is more efficient than using
  24. :const:`AF_INET`, but because the filesystem is used as the namespace
  25. for addressing, UDS is restricted to processes on the same system.
  26. The appeal of using UDS over other IPC mechanisms such as named pipes
  27. or shared memory is that the programming interface is the same as for
  28. IP networking, so the application can take advantage of efficient
  29. communication when running on a single host, but use the same code
  30. when sending data across the network.
  31. .. note::
  32. The :const:`AF_UNIX` constant is only defined on systems where UDS
  33. is supported.
  34. The socket type is usually either :const:`SOCK_DGRAM` for
  35. *user datagram protocol* (UDP) or :const:`SOCK_STREAM` for
  36. *transmission control protocol* (TCP). UDP does not require
  37. transmission handshaking or other setup, but offers lower reliability
  38. of delivery. UDP messages may be delivered out of order, more than
  39. once, or not at all. TCP, by contrast, ensures that each message is
  40. delivered exactly once, and in the correct order. Most application
  41. protocols that deliver a large amount of data, such as HTTP, are built
  42. on top of TCP. UDP is commonly used for protocols where order is less
  43. important (since the message fits in a single packet, i.e., DNS), or
  44. for *multicasting* (sending the same data to several hosts).
  45. .. note::
  46. Python's :mod:`socket` module supports other socket types but they
  47. are less commonly used, so are not covered here. Refer to the
  48. standard library documentation for more details.
  49. Looking up Hosts on the Network
  50. ===============================
  51. :mod:`socket` includes functions to interface with the domain name
  52. services on the network, to convert the host name of a server into its
  53. numerical network address. Applications do not need to convert
  54. addresses explicitly before using them to connect to a server, but it
  55. can be useful when reporting errors to include the numerical address
  56. as well as the name value being used.
  57. To find the official name of the current host, use
  58. :func:`gethostname`.
  59. .. include:: socket_gethostname.py
  60. :literal:
  61. :start-after: #end_pymotw_header
  62. The name returned will depend on the network settings for the current
  63. system, and may change if it is on a different network (such as a
  64. laptop attached to a wireless LAN).
  65. ::
  66. $ python socket_gethostname.py
  67. farnsworth.hellfly.net
  68. Use :func:`gethostbyname` to convert the name of a server to its
  69. numerical address:
  70. .. include:: socket_gethostbyname.py
  71. :literal:
  72. :start-after: #end_pymotw_header
  73. The name argument does not need to be a fully qualified name (i.e., it
  74. does not need to include the domain name as well as the base
  75. hostname). If the name cannot be found, an exception of type
  76. :class:`socket.error` is raised.
  77. .. {{{cog
  78. .. cog.out(run_script(cog.inFile, 'socket_gethostbyname.py'))
  79. .. }}}
  80. ::
  81. $ python socket_gethostbyname.py
  82. homer : ERROR: [Errno 8] nodename nor servname provided, or not known
  83. www : ERROR: [Errno 8] nodename nor servname provided, or not known
  84. www.python.org : 82.94.164.162
  85. nosuchname : ERROR: [Errno 8] nodename nor servname provided, or not known
  86. .. {{{end}}}
  87. For access to more naming information about a server, use
  88. :func:`gethostbyname_ex`. It returns the canonical hostname of the
  89. server, any aliases, and all of the available IP addresses that can be
  90. used to reach it.
  91. .. include:: socket_gethostbyname_ex.py
  92. :literal:
  93. :start-after: #end_pymotw_header
  94. Having all known IP addresses for a server lets a client implement its
  95. own load balancing or fail-over algorithms.
  96. .. {{{cog
  97. .. cog.out(run_script(cog.inFile, 'socket_gethostbyname_ex.py'))
  98. .. }}}
  99. ::
  100. $ python socket_gethostbyname_ex.py
  101. homer
  102. homer : ERROR: [Errno 8] nodename nor servname provided, or not known
  103. www
  104. www : ERROR: [Errno 8] nodename nor servname provided, or not known
  105. www.python.org
  106. Hostname: www.python.org
  107. Aliases : []
  108. Addresses: ['82.94.164.162']
  109. nosuchname
  110. nosuchname : ERROR: [Errno 8] nodename nor servname provided, or not known
  111. .. {{{end}}}
  112. Use :func:`getfqdn` to convert a partial name to a fully qualified
  113. domain name.
  114. .. include:: socket_getfqdn.py
  115. :literal:
  116. :start-after: #end_pymotw_header
  117. The name returned will not necessarily match the input argument in any
  118. way if the input is an alias, such as ``www`` is here.
  119. .. {{{cog
  120. .. cog.out(run_script(cog.inFile, 'socket_getfqdn.py'))
  121. .. }}}
  122. ::
  123. $ python socket_getfqdn.py
  124. homer : homer
  125. www : www
  126. .. {{{end}}}
  127. When the address of a server is available, use :func:`gethostbyaddr`
  128. to do a "reverse" lookup for the name.
  129. .. include:: socket_gethostbyaddr.py
  130. :literal:
  131. :start-after: #end_pymotw_header
  132. The return value is a tuple containing the full hostname, any aliases,
  133. and all IP addresses associated with the name.
  134. .. Do not cog this, since it depends on being on home network.
  135. ::
  136. $ python socket_gethostbyaddr.py
  137. Hostname : homer.hellfly.net
  138. Aliases : ['8.1.168.192.in-addr.arpa']
  139. Addresses: ['192.168.1.8']
  140. Finding Service Information
  141. ===========================
  142. In addition to an IP address, each socket address includes an integer
  143. *port number*. Many applications can run on the same host, listening
  144. on a single IP address, but only one socket at a time can use a port
  145. at that address. The combination of IP address, protocol, and port
  146. number uniquely identify a communication channel and ensure that
  147. messages sent through a socket arrive at the correct destination.
  148. Some of the port numbers are pre-allocated for a specific protocol.
  149. For example, communication between email servers using SMTP occurs
  150. over port number 25 using TCP, and web clients and servers use port 80
  151. for HTTP. The port numbers for network services with standardized
  152. names can be looked up with :func:`getservbyname`.
  153. .. include:: socket_getservbyname.py
  154. :literal:
  155. :start-after: #end_pymotw_header
  156. Although a standardized service is unlikely to change ports, looking
  157. up the value with a system call instead of hard-coding it is more
  158. flexible when new services are added in the future.
  159. .. {{{cog
  160. .. cog.out(run_script(cog.inFile, 'socket_getservbyname.py'))
  161. .. }}}
  162. ::
  163. $ python socket_getservbyname.py
  164. http : 80
  165. https : 443
  166. ftp : 21
  167. gopher : 70
  168. smtp : 25
  169. imap : 143
  170. imaps : 993
  171. pop3 : 110
  172. pop3s : 995
  173. .. {{{end}}}
  174. To reverse the service port lookup, use :func:`getservbyport`.
  175. .. include:: socket_getservbyport.py
  176. :literal:
  177. :start-after: #end_pymotw_header
  178. The reverse lookup is useful for constructing URLs to services from
  179. arbitrary addresses.
  180. .. {{{cog
  181. .. cog.out(run_script(cog.inFile, 'socket_getservbyport.py'))
  182. .. }}}
  183. ::
  184. $ python socket_getservbyport.py
  185. http://example.com/
  186. https://example.com/
  187. ftp://example.com/
  188. gopher://example.com/
  189. smtp://example.com/
  190. imap://example.com/
  191. imaps://example.com/
  192. pop3://example.com/
  193. pop3s://example.com/
  194. .. {{{end}}}
  195. The number assigned to a transport protocol can be retrieved with
  196. :func:`getprotobyname`.
  197. .. include:: socket_getprotobyname.py
  198. :literal:
  199. :start-after: #end_pymotw_header
  200. The values for protocol numbers are standardized, and defined as
  201. constants in :mod:`socket` with the prefix ``IPPROTO_``.
  202. .. {{{cog
  203. .. cog.out(run_script(cog.inFile, 'socket_getprotobyname.py'))
  204. .. }}}
  205. ::
  206. $ python socket_getprotobyname.py
  207. icmp -> 1 (socket.IPPROTO_ICMP = 1)
  208. udp -> 17 (socket.IPPROTO_UDP = 17)
  209. tcp -> 6 (socket.IPPROTO_TCP = 6)
  210. .. {{{end}}}
  211. Looking Up Server Addresses
  212. ===========================
  213. :func:`getaddrinfo` converts the basic address of a service into a
  214. list of tuples with all of the information necessary to make a
  215. connection. The contents of each tuple will vary, containing
  216. different network families or protocols.
  217. .. include:: socket_getaddrinfo.py
  218. :literal:
  219. :start-after: #end_pymotw_header
  220. This program demonstrates how to look up the connection information
  221. for ``www.python.org``.
  222. .. {{{cog
  223. .. cog.out(run_script(cog.inFile, 'socket_getaddrinfo.py'))
  224. .. }}}
  225. ::
  226. $ python socket_getaddrinfo.py
  227. Family : AF_INET
  228. Type : SOCK_DGRAM
  229. Protocol : IPPROTO_UDP
  230. Canonical name:
  231. Socket address: ('82.94.164.162', 80)
  232. Family : AF_INET
  233. Type : SOCK_STREAM
  234. Protocol : IPPROTO_TCP
  235. Canonical name:
  236. Socket address: ('82.94.164.162', 80)
  237. Family : AF_INET6
  238. Type : SOCK_DGRAM
  239. Protocol : IPPROTO_UDP
  240. Canonical name:
  241. Socket address: ('2001:888:2000:d::a2', 80, 0, 0)
  242. Family : AF_INET6
  243. Type : SOCK_STREAM
  244. Protocol : IPPROTO_TCP
  245. Canonical name:
  246. Socket address: ('2001:888:2000:d::a2', 80, 0, 0)
  247. .. {{{end}}}
  248. :func:`getaddrinfo` takes several arguments to filter the result
  249. list. The *host* and *port* values given in the example are required
  250. arguments. The optional arguments are *family*, *socktype*, *proto*,
  251. and *flags*. The family, socktype, and proto values should be ``0``
  252. or one of the constants defined by :mod:`socket`.
  253. .. include:: socket_getaddrinfo_extra_args.py
  254. :literal:
  255. :start-after: #end_pymotw_header
  256. Since *flags* includes :const:`AI_CANONNAME` the canonical name of the
  257. server (different from the value used for the lookup) is included in
  258. the results this time. Without the flag, the canonical name value is
  259. left empty.
  260. .. Do not cog, since the output depends on being on home network.
  261. ::
  262. $ python socket_getaddrinfo_extra_args.py
  263. Family : AF_INET
  264. Type : SOCK_STREAM
  265. Protocol : IPPROTO_TCP
  266. Canonical name: homer.doughellmann.com
  267. Socket address: ('192.168.1.8', 80)
  268. IP Address Representations
  269. ==========================
  270. Network programs written in C use the data type :class:`struct
  271. sockaddr` to represent IP addresses as binary values (instead of the
  272. string addresses usually found in Python programs). Convert IPv4
  273. addresses between the Python representation and the C representation
  274. with :func:`inet_aton` and :func:`inet_ntoa`.
  275. .. include:: socket_address_packing.py
  276. :literal:
  277. :start-after: #end_pymotw_header
  278. The four bytes in the packed format can be passed to C libraries,
  279. transmitted safely over the network, or saved to a database compactly.
  280. .. {{{cog
  281. .. cog.out(run_script(cog.inFile, 'socket_address_packing.py 192.168.1.1'))
  282. .. cog.out(run_script(cog.inFile, 'socket_address_packing.py 127.0.0.1', include_prefix=False))
  283. .. }}}
  284. ::
  285. $ python socket_address_packing.py 192.168.1.1
  286. Original: 192.168.1.1
  287. Packed : c0a80101
  288. Unpacked: 192.168.1.1
  289. $ python socket_address_packing.py 127.0.0.1
  290. Original: 127.0.0.1
  291. Packed : 7f000001
  292. Unpacked: 127.0.0.1
  293. .. {{{end}}}
  294. The related functions :func:`inet_pton` and :func:`inet_ntop` work
  295. with both IPv4 and IPv6 addresses, producing the appropriate format
  296. based on the address family parameter passed in.
  297. .. include:: socket_ipv6_address_packing.py
  298. :literal:
  299. :start-after: #end_pymotw_header
  300. An IPv6 address is already a hexadecimal value, so converting the
  301. packed version to a series of hex digits produces a string similar to
  302. the original value.
  303. .. {{{cog
  304. .. cog.out(run_script(cog.inFile, 'socket_ipv6_address_packing.py 2002:ac10:10a:1234:21e:52ff:fe74:40e'))
  305. .. }}}
  306. ::
  307. $ python socket_ipv6_address_packing.py 2002:ac10:10a:1234:21e:52ff:fe74\
  308. :40e
  309. Original: 2002:ac10:10a:1234:21e:52ff:fe74:40e
  310. Packed : 2002ac10010a1234021e52fffe74040e
  311. Unpacked: 2002:ac10:10a:1234:21e:52ff:fe74:40e
  312. .. {{{end}}}
  313. .. seealso::
  314. `Wikipedia: IPv6 <http://en.wikipedia.org/wiki/IPv6>`__
  315. Article discussing Internet Protocol Version 6 (IPv6).
  316. `Wikipedia: OSI Networking Model <http://en.wikipedia.org/wiki/OSI_model>`__
  317. Article describing the seven layer model of networking implementation.
  318. `Assigned Internet Protocol Numbers <http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml>`__
  319. List of standard protocol names and numbers.