I finished making a basic web server a little while ago, however I cannot access it at all. I ran it on my mac, which didn't work. I then ran it on my PC and tried to access it from my mac using Firefox.

This is the code I have for the server:

from http.server import HTTPServer, BaseHTTPRequestHandler

class RequestHandler(BaseHTTPRequestHandler):
    def _writeheaders(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_HEAD(self):
        self._writeheaders()

    def do_GET(self):
        self._writeheaders()
        self.wfile.write("""<HTML>
<HEAD><TITLE>Sample title</TITLE></HEAD>
<BODY>sample text</BODY>
</HTML>""")

serveraddr = ('', 8080)
srvr = HTTPServer(serveraddr, RequestHandler)

print("Server online and active")

srvr.serve_forever()

Here's the error I got from Firefox:

Failed to Connect

Firefox can't establish a connection to the server at ##.#.#.#:8080.
Though the site seems valid, the browser was unable to establish a connection.

* Could the site be temporarily unavailable? Try again later.
* Are you unable to browse other sites? Check the computer's network connection.
* Is your computer or network protected by a firewall or proxy? Incorrect settings can interfere with Web browsing

and here's what I got with safari:

Safari can’t connect to the server.
Safari can’t open the page “ because it could not connect to the server “##.#.#.#”.

How would I make this accessible remotely?

Thanks.

Dani AI

Generated

Short summary and likely causes (based on the thread): ’s script looks fine at a glance, but two common problems would explain “Failed to connect” even to localhost: (1) the server isn’t actually listening because the script crashed (check the terminal for a traceback), or (2) a local firewall or binding scope is preventing connections. ’s report that Vista prompted to allow 8080 and then it worked strongly suggests a firewall/permission issue for remote access; was also right to point at a code problem as a possible cause.

Practical checklist to find the real cause

  • Run the server from a terminal so you can see exceptions. If the handler raises an exception on the first request the server may stop serving.
  • Verify the process is listening on port 8080 on the host machine (Windows: netstat -ano | findstr 8080; macOS/Linux: lsof -i :8080 or ss -ltnp). If nothing listens, the script didn’t bind.
  • Confirm you’re running the intended Python interpreter (python --version vs python3 --version).
  • Test locally with a simple client: curl -v http://127.0.0.1:8080/ or telnet 127.0.0.1 8080.
  • If listening but remote clients can’t connect, allow the port/application through the OS firewall (or add an inbound rule) and, for cross-network access, set router port forwarding.

Concrete Python-3 fix to a very common issue
In Python 3, wfile is a binary stream, so writing a plain str raises a TypeError. Encode the response before writing, and bind explicitly to all interfaces when you want remote access:

# inside your handler's do_GET
self.send_response(200)
self.send_header('Content-Type', 'text/html; charset=utf-8')
self.end_headers()
body = "<html><body>sample text</body></html>"
self.wfile.write(body.encode('utf-8'))

# bind like this to accept remote connections
serveraddr = ('0.0.0.0', 8080)

Final notes: if upgrading is an option, use a maintained Python 3.x release rather than very early 3.0.x builds. Watch the console output for the exact exception — that will usually pinpoint whether it’s a code problem or an OS/network block.

Recommended Answers

All 6 Replies

I have never used a Python web server...So I wudnt know whether this works or not...

I will have to check this code in my home....

Are you able to access the page on the same box?

http://127.0.0.1:8080/

or

http://localhost:8080/

Does it work?? If it does, then could be something between the machines.....or other problems which needs to be looked into...

I tried both, neither worked. :(

What is the version of Python you use?

3.0.1

I tried both, neither worked. :(

If they're not working on the system where the server is running then your code is flawed. There was just a thread about this a few days ago that had a working server... Look there and try out that code.

Ok... here goes... I tried ur code at my home Vista.
The only change I needed to make was

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

instead of

from http.server import HTTPServer, BaseHTTPRequestHandler

Mine is 2.6, so I think it needed the change.
When I ran ur code, vista prompted me to allow the port 8080, for which I did.

Wallah!!! I see "sample text" in the web page when I use http://localhost:8080

I tried to access the vista machine with its IP from a different machine(Linux machine)... it worked again...

So I think u should check ur firewalls. I dont know about Mac, so that would be ur area to explore....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.