📄 module-socket.html
字号:
<P>
<dl><dt><b><a name='l2h-1557'><tt class='function'>gethostname</tt></a></b> ()
<dd>
Return a string containing the hostname of the machine where
the Python interpreter is currently executing. If you want to know the
current machine's IP address, use <code>gethostbyname(gethostname())</code>.
Note: <tt class="function">gethostname()</tt> doesn't always return the fully qualified
domain name; use <code>gethostbyaddr(gethostname())</code>
(see below).
</dl>
<P>
<dl><dt><b><a name='l2h-1558'><tt class='function'>gethostbyaddr</tt></a></b> (<var>ip_address</var>)
<dd>
Return a triple <code>(<var>hostname</var>, <var>aliaslist</var>,
<var>ipaddrlist</var>)</code> where <var>hostname</var> is the primary host name
responding to the given <var>ip_address</var>, <var>aliaslist</var> is a
(possibly empty) list of alternative host names for the same address,
and <var>ipaddrlist</var> is a list of IP addresses for the same interface
on the same host (most likely containing only a single address).
To find the fully qualified domain name, use the function
<tt class="function">getfqdn()</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-1559'><tt class='function'>getprotobyname</tt></a></b> (<var>protocolname</var>)
<dd>
Translate an Internet protocol name (e.g. <code>'icmp'</code>) to a constant
suitable for passing as the (optional) third argument to the
<tt class="function">socket()</tt> function. This is usually only needed for sockets
opened in ``raw'' mode (<tt class="constant">SOCK_RAW</tt>); for the normal socket
modes, the correct protocol is chosen automatically if the protocol is
omitted or zero.
</dl>
<P>
<dl><dt><b><a name='l2h-1560'><tt class='function'>getservbyname</tt></a></b> (<var>servicename, protocolname</var>)
<dd>
Translate an Internet service name and protocol name to a port number
for that service. The protocol name should be <code>'tcp'</code> or
<code>'udp'</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-1561'><tt class='function'>socket</tt></a></b> (<var>family, type</var><big>[</big><var>, proto</var><big>]</big>)
<dd>
Create a new socket using the given address family, socket type and
protocol number. The address family should be <tt class="constant">AF_INET</tt> or
<tt class="constant">AF_UNIX</tt>. The socket type should be <tt class="constant">SOCK_STREAM</tt>,
<tt class="constant">SOCK_DGRAM</tt> or perhaps one of the other "<tt class="samp">SOCK_</tt>" constants.
The protocol number is usually zero and may be omitted in that case.
</dl>
<P>
<dl><dt><b><a name='l2h-1562'><tt class='function'>fromfd</tt></a></b> (<var>fd, family, type</var><big>[</big><var>, proto</var><big>]</big>)
<dd>
Build a socket object from an existing file descriptor (an integer as
returned by a file object's <tt class="method">fileno()</tt> method). Address family,
socket type and protocol number are as for the <tt class="function">socket()</tt> function
above. The file descriptor should refer to a socket, but this is not
checked -- subsequent operations on the object may fail if the file
descriptor is invalid. This function is rarely needed, but can be
used to get or set socket options on a socket passed to a program as
standard input or output (e.g. a server started by the Unix inet
daemon).
</dl>
<P>
<dl><dt><b><a name='l2h-1563'><tt class='function'>ntohl</tt></a></b> (<var>x</var>)
<dd>
Convert 32-bit integers from network to host byte order. On machines
where the host byte order is the same as network byte order, this is a
no-op; otherwise, it performs a 4-byte swap operation.
</dl>
<P>
<dl><dt><b><a name='l2h-1564'><tt class='function'>ntohs</tt></a></b> (<var>x</var>)
<dd>
Convert 16-bit integers from network to host byte order. On machines
where the host byte order is the same as network byte order, this is a
no-op; otherwise, it performs a 2-byte swap operation.
</dl>
<P>
<dl><dt><b><a name='l2h-1565'><tt class='function'>htonl</tt></a></b> (<var>x</var>)
<dd>
Convert 32-bit integers from host to network byte order. On machines
where the host byte order is the same as network byte order, this is a
no-op; otherwise, it performs a 4-byte swap operation.
</dl>
<P>
<dl><dt><b><a name='l2h-1566'><tt class='function'>htons</tt></a></b> (<var>x</var>)
<dd>
Convert 16-bit integers from host to network byte order. On machines
where the host byte order is the same as network byte order, this is a
no-op; otherwise, it performs a 2-byte swap operation.
</dl>
<P>
<dl><dt><b><a name='l2h-1567'><tt class='function'>inet_aton</tt></a></b> (<var>ip_string</var>)
<dd>
Convert an IP address from dotted-quad string format
(e.g. '123.45.67.89') to 32-bit packed binary format, as a string four
characters in length.
<P>
Useful when conversing with a program that uses the standard C library
and needs objects of type <tt class="ctype">struct in_addr</tt>, which is the C type
for the 32-bit packed binary this function returns.
<P>
If the IP address string passed to this function is invalid,
<tt class="exception">socket.error</tt> will be raised. Note that exactly what is
valid depends on the underlying C implementation of
<tt class="cfunction">inet_aton()</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-1568'><tt class='function'>inet_ntoa</tt></a></b> (<var>packed_ip</var>)
<dd>
Convert a 32-bit packed IP address (a string four characters in
length) to its standard dotted-quad string representation
(e.g. '123.45.67.89').
<P>
Useful when conversing with a program that uses the standard C library
and needs objects of type <tt class="ctype">struct in_addr</tt>, which is the C type
for the 32-bit packed binary this function takes as an argument.
<P>
If the string passed to this function is not exactly 4 bytes in
length, <tt class="exception">socket.error</tt> will be raised.
</dl>
<P>
<dl><dt><b><a name='l2h-1569'><tt>SocketType</tt></a></b>
<dd>
This is a Python type object that represents the socket object type.
It is the same as <code>type(socket(...))</code>.
</dl>
<P>
<div class='seealso'>
<p class='heading'><b>See Also:</b></p>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-SocketServer.html" tppabs="http://www.python.org/doc/current/lib/module-SocketServer.html">SocketServer</a></tt>:</b>
<dd>Classes that simplify writing network servers.
</dl>
</div>
<P>
<p><hr>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html2835"
href="socket-objects.html" tppabs="http://www.python.org/doc/current/lib/socket-objects.html">7.2.1 Socket Objects </A>
<LI><A NAME="tex2html2836"
href="socket-example.html" tppabs="http://www.python.org/doc/current/lib/socket-example.html">7.2.2 Example </A>
</UL>
<!--End of Table of Child-Links-->
<DIV CLASS="navigation"><p><hr><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>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -