Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Create and Run a Python HTTP Server on macOS

In today's interconnected world, the ability to quickly set up a local HTTP server is invaluable for web development, testing, and sharing files. Python, with its simple and powerful libraries, makes this task straightforward. On macOS, you can leverage Python's built-in HTTP server capabilities to achieve this with minimal setup. This article will guide you through the steps to create and run a Python HTTP server on macOS, highlighting its importance and practical applications.

Examples:

  1. Setting Up Python on macOS:

    Before you can run a Python HTTP server, ensure that Python is installed on your macOS system. macOS typically comes with Python pre-installed, but you can check the version and install the latest version if necessary.

    To check if Python is installed, open the Terminal and type:

    python3 --version

    If Python is not installed, you can install it using Homebrew:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    brew install python
  2. Starting a Simple HTTP Server:

    Python's http.server module allows you to start a simple HTTP server with a single command. Navigate to the directory you want to serve and run:

    cd /path/to/your/directory
    python3 -m http.server 8000

    This command starts an HTTP server on port 8000. You can access it by opening a web browser and navigating to http://localhost:8000.

  3. Specifying a Different Port:

    If port 8000 is in use or you prefer to use a different port, you can specify it as follows:

    python3 -m http.server 8080
  4. Serving Files with HTTPS:

    For serving files securely over HTTPS, you need to generate SSL certificates. First, create a self-signed certificate using OpenSSL:

    openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes

    Then, use the following Python script to start an HTTPS server:

    import http.server
    import ssl
    
    httpd = http.server.HTTPServer(('localhost', 4443), http.server.SimpleHTTPRequestHandler)
    httpd.socket = ssl.wrap_socket(httpd.socket, certfile='./server.pem', server_side=True)
    print("Serving on https://localhost:4443")
    httpd.serve_forever()

    Save this script as https_server.py and run it:

    python3 https_server.py
  5. Customizing the HTTP Server:

    You can customize the HTTP server by creating a subclass of SimpleHTTPRequestHandler. For example, to log requests to a file:

    from http.server import SimpleHTTPRequestHandler, HTTPServer
    
    class MyHandler(SimpleHTTPRequestHandler):
       def log_message(self, format, *args):
           with open("server.log", "a") as log_file:
               log_file.write("%s - - [%s] %s\n" %
                              (self.client_address[0],
                               self.log_date_time_string(),
                               format % args))
    
    httpd = HTTPServer(('localhost', 8000), MyHandler)
    print("Serving on http://localhost:8000")
    httpd.serve_forever()

    Save this script as custom_http_server.py and run it:

    python3 custom_http_server.py

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.