简体   繁体   English

错误:提供了nodename或servname,或者未知(python套接字)

[英]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()并不总是返回完全限定的域名; use getfqdn() for that. 使用getfqdn()

The host IP is not the same as the hostname. 主机IP与主机名不同。 You have a couple of options: 你有几个选择:

  1. You can either manually assign host to 0.0.0.0 or localhost 您可以手动将host分配给0.0.0.0localhost

  2. 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.

相关问题 提供 Locust 错误节点名或服务名,或未知 - Locust Error nodename nor servname provided, or not known 提供的节点名或服务名,或者未知 - nodename nor servname provided, or not known IOError:[Errno套接字错误] [Errno 8]提供的节点名或服务名,或者未知 - IOError: [Errno socket error] [Errno 8] nodename nor servname provided, or not known 网址错误: <urlopen error [errno 8] nodename nor servname provided, or not known></urlopen> - URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known> Tweepy:[Errno 8]提供nodename或servname,或者不知道 - Tweepy: [Errno 8] nodename nor servname provided, or not known PySpark:[Errno 8] nodename 或 servname 提供,或未知 - PySpark: [Errno 8] nodename nor servname provided, or not known 提供的mongoengine节点名或servname或未知 - mongoengine nodename nor servname provided, or not known gaierror:[Errno 8]提供的节点名或服务名,或者未知 - gaierror: [Errno 8] nodename nor servname provided, or not known WSGIServerException:[Errno 8]提供了nodename或servname,或者未知 - WSGIServerException: [Errno 8] nodename nor servname provided, or not known gaierror: [Errno 8] 节点名或服务名已提供,或未知 - gaierror: [Errno 8] nodename nor servname provided, or not known
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM