| Python Library Reference |
This module provides access to the BSDsocket interface.It is available on all modern Unix systems, Windows, MacOS, BeOS,OS/2, and probably additional platforms.
For an introduction to socket programming (in C), see the followingpapers:An Introductory 4.3BSD Interprocess CommunicationTutorial, by Stuart Sechrest andAn Advanced 4.3BSDInterprocess Communication Tutorial, by Samuel J. Leffler et al,both in theUnix Programmer's Manual, Supplementary Documents 1(sections PS1:7 and PS1:8). The platform-specific reference materialfor the various socket-related system calls are also a valuable sourceof information on the details of socket semantics. For Unix, referto the manual pages; for Windows, see the WinSock (or Winsock 2)specification.For IPv6-ready APIs, readers may want to refer toRFC 2553 titledBasic Socket Interface Extensions for IPv6.
The Python interface is a straightforward transliteration of theUnix system call and library interface for sockets to Python'sobject-oriented style: thesocket() function returns asocket object whose methods implement thevarious socket system calls. Parameter types are somewhathigher-level than in the C interface: as withread() andwrite() operations on Python files, buffer allocation onreceive operations is automatic, and buffer length is implicit on sendoperations.
Socket addresses are represented as follows:A single string is used for theAF_UNIX address family.A pair(host,port) is used for theAF_INET address family, wherehost is a stringrepresenting either a hostname in Internet domain notation like'daring.cwi.nl' or an IPv4 address like'100.50.200.5',andport is an integral port number.ForAF_INET6 address family, a four-tuple(host,port,flowinfo,scopeid) isused, whereflowinfo andscopeid representssin6_flowinfo andsin6_scope_id member instruct sockaddr_in6 in C.Forsocket module methods,flowinfo andscopeidcan be omitted just for backward compatibility. Note, however,omission ofscopeid can cause problems in manipulating scopedIPv6 addresses. Other address families are currently not supported.The address format required by a particular socket object isautomatically selected based on the address family specified when thesocket object was created.
For IPv4 addresses, two special forms are accepted instead of a hostaddress: the empty string representsINADDR_ANY, and the string'<broadcast>' representsINADDR_BROADCAST.The behavior is not available for IPv6 for backward compatibility,therefore, you may want to avoid these if you intend to support IPv6 withyour Python programs.
If you use a hostname in thehost portion of IPv4/v6 socketaddress, the program may show a nondeterministic behavior, as Pythonuses the first address returned from the DNS resolution. The socketaddress will be resolved differently into an actual IPv4/v6 address,depending on the results from DNS resolution and/or the hostconfiguration. For deterministic behavior use a numeric address inhost portion.
All errors raise exceptions. The normal exceptions for invalidargument types and out-of-memory conditions can be raised; errorsrelated to socket or address semantics raise the errorsocket.error.
Non-blocking mode is supported through thesetblocking() method.
The modulesocket exports the following constants and functions:
(errno,string)representing an error returned by a systemcall, similar to the value accompanyingos.error.See the moduleerrno , which containsnames for the error codes defined by the underlying operating system.The accompanying value is a pair(h_errno,string)representing an error returned by a library call.stringrepresents the description ofh_errno, as returned bythehstrerror() C function.
(error,string)representing an error returned by a library call.string represents the description oferror, as returnedby thegai_strerror() C function.Resolves thehost/port argument, into a sequence of5-tuples that contain all the necessary argument for the socketsmanipulation.host is a domain name, a string representation ofIPv4/v6 address orNone.port is a string service name (like``http''), a numericport number orNone.
The rest of the arguments are optional and must be numeric ifspecified. Forhost andport, by passing either an emptystring orNone, you can passNULL to the C API. Thegetaddrinfo() function returns a list of 5-tuples withthe following structure:
(family,socktype,proto,canonname,sockaddr).
family,socktype,proto are all integer and are meant tobe passed to thesocket() function.canonname is a string representing the canonical name of thehost.It can be a numeric IPv4/v6 address whenAI_CANONNAME is specifiedfor a numerichost.sockaddr is a tuple describing a socket address, as described above.SeeLib/httplib.py and other library filesfor a typical usage of the function.New in version 2.2.
'100.50.200.5'. If the host nameis an IPv4 address itself it is returned unchanged. Seegethostbyname_ex() for a more complete interface.gethostbyname() does not support IPv6 name resolution, andgetaddrinfo() should be used instead for IPv4/v6 dual stack support.(hostname, aliaslist, ipaddrlist) wherehostname is the primary host name responding to the givenip_address,aliaslist is a (possibly empty) list ofalternative host names for the same address, andipaddrlist isa list of IPv4 addresses for the same interface on the samehost (often but not always a single address).gethostbyname_ex() does not support IPv6 name resolution, andgetaddrinfo() should be used instead for IPv4/v6 dual stack support.gethostbyname(gethostname()).This operation assumes that there is a valid address-to-host mapping forthe host, and the assumption does not always hold.Note:gethostname() doesn't always return the fully qualifieddomain name; usegethostbyaddr(gethostname())(see below).(hostname,aliaslist,ipaddrlist) wherehostname is the primary host nameresponding to the givenip_address,aliaslist is a(possibly empty) list of alternative host names for the same address,andipaddrlist is a list of IPv4/v6 addresses for the same interfaceon the same host (most likely containing only a single address).To find the fully qualified domain name, use the functiongetfqdn().gethostbyaddr supports both IPv4 and IPv6.(host,port).Depending on the settings offlags, the result can contain afully-qualified domain name or numeric address representation inhost. Similarly,port can contain a string port name or anumeric port number.New in version 2.2.'icmp') to a constantsuitable for passing as the (optional) third argument to thesocket() function. This is usually only needed for socketsopened in ``raw'' mode (SOCK_RAW); for the normal socketmodes, the correct protocol is chosen automatically if the protocol isomitted or zero.'tcp' or'udp'.Warning:This does not do any certificate verification!
Useful when conversing with a program that uses the standard C libraryand needs objects of typestruct in_addr, which is the C typefor the 32-bit packed binary this function returns.
If the IPv4 address string passed to this function is invalid,socket.error will be raised. Note that exactly what isvalid depends on the underlying C implementation ofinet_aton().
inet_aton() does not support IPv6, andgetnameinfo() should be used instead for IPv4/v6 dual stacksupport.
Useful when conversing with a program that uses the standard C libraryand needs objects of typestruct in_addr, which is the C typefor the 32-bit packed binary this function takes as an argument.
If the string passed to this function is not exactly 4 bytes inlength,socket.error will be raised.
inet_ntoa() does not support IPv6, andgetnameinfo() should be used instead for IPv4/v6 dual stacksupport.
type(socket(...)).See Also:
| Python Library Reference |