📄 module-socket.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>7.2 socket -- Low-level networking interface</title>
<META NAME="description" CONTENT="7.2 socket -- Low-level networking interface">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-select.html" tppabs="http://www.python.org/doc/current/lib/module-select.html">
<LINK REL="previous" href="module-signal.html" tppabs="http://www.python.org/doc/current/lib/module-signal.html">
<LINK REL="up" href="someos.html" tppabs="http://www.python.org/doc/current/lib/someos.html">
<LINK REL="next" href="socket-objects.html" tppabs="http://www.python.org/doc/current/lib/socket-objects.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="Signal_Example.html" tppabs="http://www.python.org/doc/current/lib/Signal_Example.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="someos.html" tppabs="http://www.python.org/doc/current/lib/someos.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="socket-objects.html" tppabs="http://www.python.org/doc/current/lib/socket-objects.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="Signal_Example.html" tppabs="http://www.python.org/doc/current/lib/Signal_Example.html">7.1.1 Example</A>
<b class="navlabel">Up:</b> <a class="sectref" href="someos.html" tppabs="http://www.python.org/doc/current/lib/someos.html">7. Optional Operating System</A>
<b class="navlabel">Next:</b> <a class="sectref" href="socket-objects.html" tppabs="http://www.python.org/doc/current/lib/socket-objects.html">7.2.1 Socket Objects</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION009200000000000000000">
7.2 <tt class="module">socket</tt> --
Low-level networking interface</A>
</H1>
<P>
<P>
This module provides access to the BSD <i>socket</i> interface.
It is available on all modern Unix systems, Windows, MacOS, BeOS,
OS/2, and probably additional platforms.
<P>
For an introduction to socket programming (in C), see the following
papers: <em class='citetitle'
>An Introductory 4.3BSD Interprocess Communication
Tutorial</em>, by Stuart Sechrest and <em class='citetitle'
>An Advanced 4.3BSD
Interprocess Communication Tutorial</em>, by Samuel J. Leffler et al,
both in the <em class='citetitle'
>Unix Programmer's Manual, Supplementary Documents 1</em>
(sections PS1:7 and PS1:8). The platform-specific reference material
for the various socket-related system calls are also a valuable source
of information on the details of socket semantics. For Unix, refer
to the manual pages; for Windows, see the WinSock (or Winsock 2)
specification.
<P>
The Python interface is a straightforward transliteration of the
Unix system call and library interface for sockets to Python's
object-oriented style: the <tt class="function">socket()</tt> function returns a
<i class="dfn">socket object</i> whose methods implement the
various socket system calls. Parameter types are somewhat
higher-level than in the C interface: as with <tt class="method">read()</tt> and
<tt class="method">write()</tt> operations on Python files, buffer allocation on
receive operations is automatic, and buffer length is implicit on send
operations.
<P>
Socket addresses are represented as a single string for the
<tt class="constant">AF_UNIX</tt> address family and as a pair
<code>(<var>host</var>, <var>port</var>)</code> for the <tt class="constant">AF_INET</tt> address
family, where <var>host</var> is a string representing
either a hostname in Internet domain notation like
<code>'daring.cwi.nl'</code> or an IP address like <code>'100.50.200.5'</code>,
and <var>port</var> is an integral port number. Other address families are
currently not supported. The address format required by a particular
socket object is automatically selected based on the address family
specified when the socket object was created.
<P>
For IP addresses, two special forms are accepted instead of a host
address: the empty string represents <tt class="constant">INADDR_ANY</tt>, and the string
<code>'<broadcast>'</code> represents <tt class="constant">INADDR_BROADCAST</tt>.
<P>
All errors raise exceptions. The normal exceptions for invalid
argument types and out-of-memory conditions can be raised; errors
related to socket or address semantics raise the error
<tt class="exception">socket.error</tt>.
<P>
Non-blocking mode is supported through the
<tt class="method">setblocking()</tt> method.
<P>
The module <tt class="module">socket</tt> exports the following constants and functions:
<P>
<dl><dt><b><a name='l2h-1550'><tt class='exception'>error</tt></a></b>
<dd>
This exception is raised for socket- or address-related errors.
The accompanying value is either a string telling what went wrong or a
pair <code>(<var>errno</var>, <var>string</var>)</code>
representing an error returned by a system
call, similar to the value accompanying <tt class="exception">os.error</tt>.
See the module <tt class='module'><a href="module-errno.html" tppabs="http://www.python.org/doc/current/lib/module-errno.html">errno</a></tt>, which contains
names for the error codes defined by the underlying operating system.
</dl>
<P>
<dl><dt><b><a name='l2h-1551'><tt>AF_UNIX</tt></a></b>
<dd>
<dt><b><a name='l2h-1572'><tt>AF_INET</tt></a></b><dd>
These constants represent the address (and protocol) families,
used for the first argument to <tt class="function">socket()</tt>. If the
<tt class="constant">AF_UNIX</tt> constant is not defined then this protocol is
unsupported.
</dl>
<P>
<dl><dt><b><a name='l2h-1552'><tt>SOCK_STREAM</tt></a></b>
<dd>
<dt><b><a name='l2h-1573'><tt>SOCK_DGRAM</tt></a></b><dd>
<dt><b><a name='l2h-1574'><tt>SOCK_RAW</tt></a></b><dd>
<dt><b><a name='l2h-1575'><tt>SOCK_RDM</tt></a></b><dd>
<dt><b><a name='l2h-1576'><tt>SOCK_SEQPACKET</tt></a></b><dd>
These constants represent the socket types,
used for the second argument to <tt class="function">socket()</tt>.
(Only <tt class="constant">SOCK_STREAM</tt> and
<tt class="constant">SOCK_DGRAM</tt> appear to be generally useful.)
</dl>
<P>
<dl><dt><b><a name='l2h-1553'><tt>SO_*</tt></a></b>
<dd>
<dt><b><a name='l2h-1577'><tt>SOMAXCONN</tt></a></b><dd>
<dt><b><a name='l2h-1578'><tt>MSG_*</tt></a></b><dd>
<dt><b><a name='l2h-1579'><tt>SOL_*</tt></a></b><dd>
<dt><b><a name='l2h-1580'><tt>IPPROTO_*</tt></a></b><dd>
<dt><b><a name='l2h-1581'><tt>IPPORT_*</tt></a></b><dd>
<dt><b><a name='l2h-1582'><tt>INADDR_*</tt></a></b><dd>
<dt><b><a name='l2h-1583'><tt>IP_*</tt></a></b><dd>
Many constants of these forms, documented in the Unix documentation on
sockets and/or the IP protocol, are also defined in the socket module.
They are generally used in arguments to the <tt class="method">setsockopt()</tt> and
<tt class="method">getsockopt()</tt> methods of socket objects. In most cases, only
those symbols that are defined in the Unix header files are defined;
for a few symbols, default values are provided.
</dl>
<P>
<dl><dt><b><a name='l2h-1554'><tt class='function'>getfqdn</tt></a></b> (<big>[</big><var>name</var><big>]</big>)
<dd>
Return a fully qualified domain name for <var>name</var>.
If <var>name</var> is omitted or empty, it is interpreted as the local
host. To find the fully qualified name, the hostname returned by
<tt class="function">gethostbyaddr()</tt> is checked, then aliases for the host, if
available. The first name which includes a period is selected. In
case no fully qualified domain name is available, the hostname is
returned.
New in version 2.0.
</dl>
<P>
<dl><dt><b><a name='l2h-1555'><tt class='function'>gethostbyname</tt></a></b> (<var>hostname</var>)
<dd>
Translate a host name to IP address format. The IP address is
returned as a string, e.g., <code>'100.50.200.5'</code>. If the host name
is an IP address itself it is returned unchanged. See
<tt class="function">gethostbyname_ex()</tt> for a more complete interface.
</dl>
<P>
<dl><dt><b><a name='l2h-1556'><tt class='function'>gethostbyname_ex</tt></a></b> (<var>hostname</var>)
<dd>
Translate a host name to IP address format, extended interface.
Return a triple <code>(hostname, aliaslist, ipaddrlist)</code> where
<code>hostname</code> is the primary host name responding to the given
<var>ip_address</var>, <code>aliaslist</code> is a (possibly empty) list of
alternative host names for the same address, and <code>ipaddrlist</code> is
a list of IP addresses for the same interface on the same
host (often but not always a single address).
</dl>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -