Advertisement
rfmonk

keyboard_drill_sockets.txt

May 11th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.81 KB | None | 0 0
  1. socket
  2.  
  3. 17.2 socket - Low-level networking interface
  4.  
  5. This module provides access to the BSD socket interface. It is available
  6. on all modern UNIX systems, Windows, Mac OS X, BeOS, OS/2, and probably
  7. additional platforms.
  8.  
  9. Note: Some behavior may be platform dependent, since calls are made to
  10. the operating system socket APIs.
  11.  
  12. For an introduction to socket programming (in C), see the following papers:
  13. An introductory 4.3BSD Interprocess Communication Tutorial, by Stuart Sechrest
  14. and An Advanced 4.3BSD Interprocess Communication Tutorial, by Samuel J.
  15. Leffler et al, both in the UNIX Programmer's Manual, Supplementary Documents 1
  16. (sections PS1:7 and PS1:8). The platform-specific reference material for the
  17. various socket related system calls are also a valuable source of information
  18. on the details of socket semantics. For Unix, refer to the manual pages.
  19.  
  20. The Python interface is a straightforward transliteration of the Unix
  21. system call and library interface for sockets to Python's object-oriented
  22. style: the socket() function returns a socket whose methods implement
  23. the various socket system calls. Parameter types are somewhat higher-level
  24. than in the C interface: as with read() and write() operations on Python files
  25. buffer allocation on receive operations is automatic, and buffer length is
  26. implicit on send operations.
  27.  
  28. Socket addresses are represented as follows: A single string is used for the
  29. AF_INET address family, where the host is a string representing either
  30. a hostname in Internet domain notation like 'daring.cwi.nl' or an IPv4
  31. address like '100.50.200.5', and port is an integer. For AF_INET6 address
  32. family, a four-tuple (host, port, flowinfo, scopeid) is used, where flowinfo
  33. and scopeid represents sin6_flowinfo and sin6_scope_id member in struct
  34. sockaddr_in6 in C. For socket module methods, flowinfo and scopeid can be
  35. ommitted for backward compatibility. Note however, omission of scopeid can
  36. cause problems in manipulating scoped ipv6 addresses. Other address families
  37. are not currently supported. The address format required by a particular
  38. socket object is automatically selected based on the address family specified
  39. when the socket object was created.
  40.  
  41. For IPv4 addresses, two special forms are accepted instead of a host address:
  42. the empty string represents INADDR_ANY, and the string '<broadcast>'
  43. represents INADDR_BROADCAST. The behavior is not available for IPv6 for
  44. backward compatibility, therefore you may want to avoid these if you intend
  45. to support IPv6 with your Python programs.
  46.  
  47. If you use a hostname in the portion of IPv4/v6 socket address, the program
  48. may show a nondeterministic behavior, as Python uses the first address
  49. returned from the DNS resolution. The socket address will be resolved
  50. differently into an actual IPv4/v6 address, depending on the results from DNS
  51. resolution and or the host configuration. For deterministic behavior use a
  52. numeric address in host portion.
  53.  
  54. New in version 2.5:AF_NETLINK sockets are represented as pairs pid, groups.
  55.  
  56. New in version 2.6: Linux-only support for TIPC is also available using the
  57. AF_TIPC address family. TIPC is an open, non-IP based networked protocol
  58. designed for use in clustered computer environments. Addresses are
  59. represented by a tuple, and the fields depend on the address type. The
  60. general tuple form is (addr_type, v1, v2, v3 [, scope]), where:
  61.  
  62. * addr_type is one of TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, or TIPC_ADDR_ID.
  63. * scope is one of TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
  64. * if addr_type is TIPC_ADDR_NAME, then v1 is the server type, v2 is the port identifier, and v3 should be 0
  65. * if addr_type is TIPC_ADDR_NAMESEQ, then v1 is the server type, v2 is the lower port number, and v3 is the upper port number.
  66. * if addr_type is TIPC_ADDR_ID, then v1 is the node, v2 is the reference, and v3 should be set to 0.
  67.  
  68. All errors raise exceptions. The normal exceptions for invalid argument types
  69. and out-of-memory conditions can be raised; errors related to socket or
  70. address semantics raise the error socket.error.
  71.  
  72. Non-blocking mode is supported through setblocking(). A generalization of
  73. this based on the timeouts is supported through settimeout().
  74.  
  75. The module socket exports the following constants and functions:
  76.  
  77. exception socket.error
  78. This exception is raised for socket-related errors. The accompanying
  79. value is either a string telling what went wrong or a pair (erron, string)
  80. representing an error returned by a system call, similar to the value
  81. accompanying os.error. See the module errno, which contains names for the
  82. error codes defined by the underlying operating system.
  83.  
  84. changed in version 2.6: socket.error is no a child class of IOError.
  85.  
  86. exception socket.herror
  87. This exception is raised for address-related errors, i.e. for functions
  88. that use h_errno in the C API, including gethostbyname_ex() and
  89. gethostbyaddr().
  90.  
  91. The accompanying value is a pair (h_errno, string) representing an error returned by a library call. string represents the description of h_errno, as returned by the hstrerror() C function.
  92.  
  93. exception socket.gaierror
  94. This exception is raised for address-related errors, for getaddrinfo()
  95. and getnameinfo(). The accompanying value is a pair (error, string)
  96. representing an error returned by a library call. string represents the
  97. description of error, as returned by the gai_strerror()C function. The
  98. error value will match one of the EAI_* constants defined in this module.
  99.  
  100. exception socket.timeout
  101. This exception is raised when a timeout occurs on a socket which has had
  102. timeouts enabled via a prior call to settimeout(). The accompanying
  103. value is a string whose value is currently always "timed out".
  104.  
  105. New in version 2.3
  106.  
  107. socket.AF_UNIX
  108. socket.AF_INET
  109. socket.AF_INET6
  110. These constants represent the address and protocol families, used for
  111. the first argument to socket(). If the AF_UNIX constant is not defined
  112. then this protocol is unsupported.
  113.  
  114. socket.SOCK_STREAM
  115. socket.SOCK_DGRAM
  116. socket.SOCK_RAW
  117. socket.SOCK_RDM
  118. socket.SOCK_SEQPACKET
  119. These constants represent the socket types, used for the second argument to socket(). (Only SOCK_STREAM and SOCK_DGRAM appear to be generally useful.)
  120.  
  121. SO_*
  122. socket.SOMAXCONN
  123. MSG_*
  124. SOL_*
  125. IPPROTO_*
  126. IPPORT_*
  127. INADDR_*
  128. IP_*
  129. IPV6_*
  130. EAI_*
  131. AI_*
  132. NI_*
  133. TCP_*
  134. Many constants of these forms, documented in the Unix documentation on
  135. sockets and/or IP protocol, are also defined in the socket module. They
  136. are generally used in arguments to the setsockopt() and getsockopt()
  137. methods of socket objects. In most cases, only those symbols that are
  138. defined in the Unix header files are defined; for a few symbols, default
  139. values are provided.
  140.  
  141. SIO_*
  142. RCVALL_*
  143. Constant for Windows' WSAloctl(). The constants are used as arguments to
  144. the ioctl() method of socket objects.
  145.  
  146. New in version 2.6
  147.  
  148. TIPC_*
  149. TIPC related constants, matching the ones exported by the C socket API. See the TIPC documentation for more information.
  150.  
  151. New in version 2.6
  152.  
  153. socket.has_ipv6
  154. This constant contains a boolean value which indicates if IPv6 is supported on this platform.
  155.  
  156. New in version 2.3
  157.  
  158. socket.create_connection(address[, timeout[, source_address]])
  159. Connect to a TCP service listening on the Internet address (a 2-tuple (
  160. host, port)), and return the socket object. This is a higher-level
  161. function than socket.connect(): if host is a non-numeric hostname, it
  162. will try to resolve it for both AF_INET and AF_INET6, and then try to
  163. connect to all possible addresses in turn until a connection succeeds.
  164. This makes it easy to write clients that are compatible to both IPv4 and
  165. IPv6.
  166.  
  167. Passing the optional timeout parameter will set the timeout on the
  168. socket instance before attempting to connect. If no timeout is supplied,
  169. the global default timeout setting returned by getdefaulttimeout() is
  170. used.
  171.  
  172. If supplied, source_address must be a 2-tuple (host, port) for the
  173. socketto bind to as its source address before connecting. If host or
  174. port are '' or 0 respectively the OS default behavior will be used.
  175.  
  176. New in version 2.6
  177.  
  178. Changed in version 2.7: source_address was added.
  179.  
  180. socket.getaddrinfo(host, port[, family[, socktype[, proto[, flags]]]])
  181. Translate the host/port argument into a sequence of 5-tuples that
  182. contain all the necessary arguments for creating a socket connected to
  183. that service. host is a domain name, a string representation of an
  184. IPv4/v6 address or None. port is a string service name such as 'http', a
  185. numeric port number or None. By passing None as the value of host and
  186. port, you can pass NULL to the underlying C API.
  187.  
  188. The family, socktype and proto arguments can be optionally specified in
  189. order to narrow the list of addresses returned. By default, their value
  190. is 0, meaning that the full range of results is selected. The flags
  191. argument can be one or several of the AI_* constants, and will influence
  192. how results are computed and returned. Its default value is 0. For
  193. example, AI_NUMERICHOST will disable domain name resolution and will
  194. raise an error if the host is a domain name.
  195.  
  196. The function returns a list of 5-tuples with the following structure:
  197.  
  198. (family, socktype, proto, canonname, sockaddr)
  199.  
  200. If these tuples, family, socktype, proto are all integers and are meant
  201. to be passed to the socket() function canonname will be empty. sockaddr
  202. is a tuple describing a socket address, whose format depends on the
  203. returned family (a (address, port) 2-tuple for AF_INET, a (address,
  204. port, flow info, scope id) 4-tuple for AF_INET6), and is meant to be
  205. passed to the socket.connect() method.
  206.  
  207. The following example fetches address information for a hypothetical TCP
  208. connection to www.python.org on port 80 (results may differ on your
  209. system if IPv6 isn't enabled):
  210.  
  211. >>> socket.getaddrinfo("www.python.org", 80, 0, 0, socket.SOL_TCP)
  212. [(2, 1, 6, '', ('82.94.164.162', 80)),
  213. (10, 1, 6, '', ('2001:888:2000:d::a2', 80, 0, 0))]
  214.  
  215. socket.getfqdn([name])
  216. Return a fully qualified domain name for name. If name is omitted or
  217. empty, it is interpreted as the local host. To find the fully qualified
  218. name, the hostname returned by gethostbyaddr() is checked, followed by
  219. aliases for the host, if available. The first name which includes a
  220. period is selected. In case no fully qualified domain name is available,
  221. the hostname as returned by gethostname() is returned.
  222.  
  223. socket.gethostbyname(hostname)
  224. translate a host name to IPv4 address format. The IPv4 address is
  225. returned as a string, such as '100.50.200.5'. If the host name is an
  226. IPv4 address itself it is returned unchanged. See gethostbyname_ex() for
  227. a more complete interface. gethostbyname() does not support IPv6 name
  228. resolution, and getaddrinfo() should be used instead for IPv4/v6 dual
  229. stack support.
  230.  
  231. socket.gethostbyname_ex(hostname)
  232. Translate a host name to IPv4 address format, extended interface. Return
  233. a triple (hostname, aliaslist, ipaddrlist) where hostname is the primary
  234. host name responding to the given ip_address, aliaslist is a possibly
  235. empty list of alternative host names for the same address, and
  236. ipaddrlist is a list of IPv4 addresses for the same interface on the
  237. same host (often but not always a single address). gethostbyname_ex()
  238. does not support IPv6 name resolution, and getaddrinfo()should be used
  239. instead for IPv4/v6 dual stack support.
  240.  
  241. socket.gethostname()
  242. Return a string containing the hostname of the machine where the Python
  243. interpreter is currently executing.
  244.  
  245. If you want to know the current machines IP address, you may want to use
  246. gethostbyname(gethostname()). This operation assumes that there is a
  247. valid address-to-host mapping for the host, and the assumption does not
  248. always hold.
  249.  
  250. Note: gethostname() doesn't always return the fqdn; use getfqdn() (see
  251. above).
  252.  
  253. socket.gethostbyaddr(ip_address)
  254. Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the
  255. primary host name responding to the given ip_address, aliaslist is a (
  256. possibly empty) list of alternative host names for the same address, and
  257. ipaddrlist is a list of IPv4/v6 addresses for the same interface on the
  258. same host (most likely containing only a single address). To find the
  259. fully qualified domain name, use the function getfqdn(). gethostbyaddr()
  260. supports both IPv4 and IPv6.
  261.  
  262. socket.getnameinfo(sockaddr, flags)
  263. Translate a socket address sockaddr into a 2-tuple (host, port).
  264. Depending on the settings of flags, the result can contain a fully-
  265. qualified domain name or numeric address representation in host.
  266. Similarly, port can contain a string port name or a numeric port number.
  267.  
  268. socket.getprotobyname(protocolname)
  269. Translate an Internet protocol name (for example, 'icmp') to a constant
  270. suitable for passing as the (optional) third argument to the socket()
  271. function. This is usually only needed for sockets opened in "raw" mode (
  272. SOCK_RAW); for the normal socket modes, the correct protocol is chosen
  273. automatically if the protocol is omitted or zero.
  274.  
  275. socket.getservbyname(servicename[, protocolname])
  276. Translate an Internet service name and protocol name to a port number
  277. for that service. The optional protocol name, if given, should be 'tcp'
  278. or 'udp', otherwise any protocol will match.
  279.  
  280. socket.getservbyport(port[, protocolname])
  281. Translate an Internet port number and protocol name to a service name
  282. for that service. The optional protocol name, if given, should be 'tcp'
  283. or 'udp', otherwise any protocol will match.
  284.  
  285. socket.socket([family[, type[, proto]]])
  286. Create a new socket using the given address family, socket type and
  287. protocol number. The address family should be AF_INET (the default),
  288. AF_INET6 or AF_UNIX. The socket type should be SOCK_STREAM (the default)
  289. , SOCK_DGRAM or perhaps one of the other sock_ constants. The protocol
  290. number is usually zero and may be omitted in that case.
  291.  
  292. socket.socketpair([family[, type[, proto]]])
  293. Build a pair of connected socket objects using the given address family,
  294. socket type, and protocol number. Address family, socket type and
  295. protocol number are as for the socket() function above. The default
  296. family is AF_UNIX if defined on the platform; otherwise the default is
  297. AF_INET. Availability: Unix.
  298.  
  299.  
  300. socket.fromfd(fd, family, type[, proto])
  301. Duplicate the file descriptor fd (an integer as returned by a file
  302. object’s fileno() method) and build a socket object from the result.
  303. Address family, socket type and protocol number are as for the socket()
  304. function above. The file descriptor should refer to a socket, but this
  305. is not checked — subsequent operations on the object may fail if the
  306. file descriptor is invalid. This function is rarely needed, but can be
  307. used to get or set socket options on a socket passed to a program as
  308. standard input or output (such as a server started by the Unix inet
  309. daemon). The socket is assumed to be in blocking mode. Availability: Unix
  310.  
  311.  
  312. socket.ntohl(x)
  313. Convert 32-bit positive integers from network to host byte order. On
  314. machines where the host byte order is the same as network byte order,
  315. this is a no-op; otherwise, it performs a 4-byte swap operation.
  316.  
  317. socket.ntohs(x)
  318. Convert 16-bit positive integers from network to host byte order. On
  319. machines where the host byte order is the same as network byte order,
  320. this is a no-op; otherwise, it performs a 2-byte swap operation.
  321.  
  322. socket.htonl(x)
  323. Convert 32-bit positive integers from host to network byte order. On
  324. machines where the host byte order is the same as network byte order,
  325. this is a no-op; otherwise, it performs a 4-byte swap operation.
  326.  
  327. socket.htons(x)
  328. Convert 16-bit positive integers from host to network byte order. On
  329. machines where the host byte order is the same as network byte order,
  330. this is a no-op; otherwise, it performs a 2-byte swap operation.
  331.  
  332. socket.inet_aton(ip_string)
  333. Convert an IPv4 address from dotted-quad string format (for example, ‘123
  334. .45.67.89’) to 32-bit packed binary format, as a string four characters
  335. in length. This is useful when conversing with a program that uses the
  336. standard C library and needs objects of type struct in_addr, which is
  337. the C type for the 32-bit packed binary this function returns.
  338.  
  339. inet_aton() also accepts strings with less than three dots; see the Unix
  340. manual page inet(3) for details.
  341.  
  342. If the IPv4 address string passed to this function is invalid, socket.
  343. error will be raised. Note that exactly what is valid depends on the
  344. underlying C implementation of inet_aton().
  345.  
  346. inet_aton() does not support IPv6, and inet_pton() should be used
  347. instead for IPv4/v6 dual stack support.
  348.  
  349.  
  350. socket.inet_ntoa(packed_ip)
  351. Convert a 32-bit packed IPv4 address (a string four characters in length)
  352. to its standard dotted-quad string representation (for example, ‘123.45.
  353. 67.89’). This is useful when conversing with a program that uses the
  354. standard C library and needs objects of type struct in_addr, which is
  355. the C type for the 32-bit packed binary data this function takes as an
  356. argument.
  357.  
  358. If the string passed to this function is not exactly 4 bytes in length,
  359. socket.error will be raised. inet_ntoa() does not support IPv6, and
  360. inet_ntop() should be used instead for IPv4/v6 dual stack support.
  361.  
  362. socket.inet_pton(address_family, ip_string)
  363. Convert an IP address from its family-specific string format to a
  364. packed, binary format. inet_pton() is useful when a library or network
  365. protocol calls for an object of type struct in_addr (similar to inet_aton
  366. ()) or struct in6_addr.
  367.  
  368. Supported values for address_family are currently AF_INET and AF_INET6.
  369. If the IP address string ip_string is invalid, socket.error will be
  370. raised. Note that exactly what is valid depends on both the value of
  371. address_family and the underlying implementation of inet_pton().
  372.  
  373. Availability: Unix (maybe not all platforms).
  374.  
  375. socket.inet_ntop(address_family, packed_ip)
  376. Convert a packed IP address (a string of some number of characters) to
  377. its standard, family-specific string representation (for example, '7.10.0
  378. .5' or '5aef:2b::8') inet_ntop() is useful when a library or network
  379. protocol returns an object of type struct in_addr (similar to inet_ntoa()
  380. ) or struct in6_addr.
  381.  
  382. Supported values for address_family are currently AF_INET and AF_INET6.
  383. If the string packed_ip is not the correct length for the specified
  384. address family, ValueError will be raised. A socket.error is raised for
  385. errors from the call to inet_ntop().
  386.  
  387. Availability: Unix (maybe not all platforms).
  388.  
  389. socket.getdefaulttimeout()
  390. Return the default timeout in seconds (float) for new socket objects. A
  391. value of None indicates that new socket objects have no timeout. When
  392. the socket module is first imported, the default is None.
  393.  
  394. socket.setdefaulttimeout(timeout)
  395. Set the default timeout in seconds (float) for new socket objects. A
  396. value of None indicates that new socket objects have no timeout. When
  397. the socket module is first imported, the default is None.
  398.  
  399. socket.SocketType
  400. This is a Python type object that represents the socket object type. It
  401. is the same as type(socket(...)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement