gpt4 book ai didi

What does this socket.gaierror mean?(这个socket.gaierror是什么意思?)

转载 作者:bug小助手 更新时间:2023-10-25 18:41:28 32 4
gpt4 key购买 nike



I'm new to python and going through a book, Core Python Applications 3rd Edition. This is the the first example and already I'm stumped with it. Here's the code with the error at the end.

我是新手,正在阅读一本名为《核心Python应用程序第三版》的书。这是第一个例子,我已经被它难住了。以下是代码末尾的错误。



#!/usr/bin/env python

from socket import *
from time import ctime

HOST = ' '
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
print 'waiting for connection...'
tcpCliSock, addr = tcpSerSock.accept()
print "...connected from:", addr

while True:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
tcpCliSock.send("[%s] %s" % (ctime(), data))

tcpCliSock.close()
tcpSerSock.close()

Traceback (most recent call last):
File "tsTserv.py", line 12, in <module>
tcpSerSock.bind(ADDR)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
socket.gaierror: [Errno 8] nodename nor servname provided, or not known


What does this mean?

这是什么意思?


更多回答
优秀答案推荐

It means that your given host name ' ' is invalid (gai stands for getaddrinfo()).

这意味着给定的主机名“"无效(gai代表getaddrinfo())。



As NPE already states, maybe an empty string '' would be more appropriate than a space ' '.

正如NPE已经指出的,也许空字符串‘’比空格‘’更合适。



The



HOST = ' '


should read

应阅读



HOST = ''


(i.e. no space between the quotes).

(即引号之间没有空格)。



The reason you're getting the error is that ' ' is not a valid hostname. In this context, '' has a special meaning (it basically means "all local addresses").

出现该错误的原因是‘’不是有效的主机名。在这种情况下,‘’有特殊的含义(它基本上意味着“所有本地地址”)。



Your code connects to a host itself on a port "PORT" with TCP protocol and sends itself a message where the network buffer size is 1024. The error is due to the space. It should be "HOST = ''" as this will connect it to itself. Alternatively, I think you could use "localhost" or "127.0.0.1".

您的代码通过使用TCP协议的端口“Port”连接到主机本身,并向自身发送一条网络缓冲区大小为1024的消息。错误是由于空格造成的。它应该是“host=‘’”,因为这将把它连接到它自己。或者,我认为您可以使用“本地主机”或“127.0.0.1”。


更多回答

This answer is more useful, for everybody except the OP - that is people, who haven't written HOST = ' ' in their small TCP echo server. Me for example - I just wanted to know what kind of exception this was. So thanks for explaining what gai stands for.

这个答案对每个人都更有用,除了OP--也就是那些没有在他们的小tcp回显服务器中写入host=‘’的人。以我为例,我只是想知道这是哪种例外。因此,谢谢你解释了GAI代表的是什么。

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com