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 “http://##.#.#.#:8080/” because it could not connect to the server “##.#.#.#”.

How would I make this accessible remotely?

Thanks.

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.