Python Forum
socket.gaierror: [Errno -2] Name or Service not known - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: socket.gaierror: [Errno -2] Name or Service not known (/thread-8647.html)



socket.gaierror: [Errno -2] Name or Service not known - ItsAGuyaneseTing - Mar-01-2018

Hello, so I'm using Python 3.4 and currently I'm trying to get this code that I found to work. So what I want to do is log some information / access it from another location. So this is the code that I'm currently using:

#!/usr/bin/python3.4
import socketserver
import http.server

class HTTPHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == "/admin":
self.wfile.write("This Page is For Admins Only!!\n")
self.wfile.write(self.headers)
else:
http.server.SimpleHTTPRequestHandler.do_GET(self)

http_Server = socketserver.TCPServer((" ", 4444), HTTPHandler)
http_Server.server_forever()

But it gives me this error:
socket.gaierror: [Errno -2] Name or Service not known

Can someone explain to me what this error means and how I can fix this issue?
Thanks Big Grin


RE: socket.gaierror: [Errno -2] Name or Service not known - mpd - Mar-02-2018

http_Server = socketserver.TCPServer((" ", 4444), HTTPHandler)
That empty string is your problem. You need to give the server a hostname or interface for it to "listen" for incoming connections. If you are doing all of this local to your computer, you can specify "localhost" or "127.0.0.1", if you want to access it from another computer, you can specify "0.0.0.0" which means accept connections on any network interface.