[英]Error: nodename nor servname provided, or not known (python sockets)
I am trying to learn how to use sockets using this tutorial: 我正在尝试使用本教程学习如何使用套接字:
https://www.tutorialspoint.com/python/python_networking.htm https://www.tutorialspoint.com/python/python_networking.htm
I have copied the code from the site into my directory and ran it exactly as was done in the tutorial but got errors. 我已经将代码从站点复制到我的目录中,并且完全像在教程中那样运行,但是出现了错误。 Here is the code from the tutorial.
这是教程中的代码。
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = 'localhost' # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print("asdf")
c.send('Thank you for connecting')
c.close() # Close the connection
and client.py 和client.py
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
s.close # Close the socket when done
These are the console commands I ran: 这些是我运行的控制台命令:
python server.py &
python client.py
I got this errors after running the command: 运行命令后出现此错误:
Traceback (most recent call last):
File "client.py", line 9, in <module>
s.connect((host, port))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/soc ket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
In case this is helpful, the version of python I am using is Python 2.7.10 and I use a mac that is version 10.12.6 如果这有用,我使用的python版本是Python 2.7.10,我使用的是版本10.12.6的mac
Thanks in advance 提前致谢
From the docs of socket.gethostname
: 来自
socket.gethostname
的文档:
Return a string containing the hostname of the machine where the Python interpreter is currently executing.
返回一个字符串,其中包含Python解释器当前正在执行的机器的主机名。
Note:
gethostname()
doesn't always return the fully qualified domain name;注意:
gethostname()
并不总是返回完全限定的域名; usegetfqdn()
for that.使用
getfqdn()
。
The host IP is not the same as the hostname. 主机IP与主机名不同。 You have a couple of options:
你有几个选择:
You can either manually assign host
to 0.0.0.0
or localhost
您可以手动将
host
分配给0.0.0.0
或localhost
You can also query socket.gethostbyname
: 您还可以查询
socket.gethostbyname
:
host = socket.gethostbyname(socket.gethostname()) # or socket.getfqdn() if the former doesn't work
I did some changes in your code. 我对你的代码做了一些修改。 Here's the server.py
这是server.py
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5)
c,addr= s.accept()
print "Got connection from the ", addr
c.send('Thank you for connecting')
c.close() # Close the connection
Here's the client.py 这是client.py
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
msg = (s.recv(1024))
print msg
s.close # Close the socket when done
I hope it will help 我希望它会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.