Python is a popular programming language. It is used for many things, including web development, data science, and machine learning.

Simple output

import base64

print(base64.b64decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'))

Save to file

import base64

with open('empty.gif', 'wb') as f:
    f.write(base64.b64decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'))

AsyncIO HTTP Server

AsyncIO is a library for writing asynchronous code in Python. It is used in many popular Python web frameworks.

import asyncio
import base64

async def handle_echo(reader, writer):
    data = base64.b64decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7')
    writer.write(data)
    await writer.drain()
    writer.close()

loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_echo, "127.0.0.1", 8888, loop=loop)
loop.run_until_complete(coro)
loop.run_forever()

FastAPI

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.

from fastapi import FastAPI, Response

app = FastAPI()

@app.get("/empty-gif")
async def empty_gif():
    gif_bytes = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
    return Response(content=gif_bytes, media_type='image/gif')

Flask

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.

from flask import Flask, Response

app = Flask(__name__)

@app.route("/empty-gif")
def empty_gif():
    gif_bytes = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
    return Response(gif_bytes, mimetype='image/gif')

Django

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. It takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

from django.http import HttpResponse

def empty_gif(request):
    gif_bytes = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
    return HttpResponse(gif_bytes, content_type='image/gif')

urlpatterns = [
    path('empty-gif', empty_gif),
]

Pyramid

Pyramid is a Python web framework that is designed to be fast, secure, and scalable. It is a good choice for building large, complex web applications.

from pyramid.response import Response

def empty_gif(request):
    gif_bytes = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
    return Response(gif_bytes, content_type='image/gif')

config = Configurator()
config.add_route('empty_gif', '/empty-gif')
config.add_view(empty_gif, route_name='empty_gif')
app = config.make_wsgi_app()

CherryPy

CherryPy is a Pythonic, object-oriented HTTP framework. It is the underlying engine for the Pylons web framework, and can be used to build any type of web application. It is fast, thread-safe, and scalable.

import cherrypy

class EmptyGif(object):
    @cherrypy.expose
    def index(self):
        gif_bytes = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
        cherrypy.response.headers['Content-Type'] = 'image/gif'
        return gif_bytes

cherrypy.quickstart(EmptyGif())

Tornado

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed. By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.

import tornado.ioloop
import tornado.web

class EmptyGifHandler(tornado.web.RequestHandler):
    def get(self):
        gif_bytes = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
        self.set_header('Content-Type', 'image/gif')
        self.write(gif_bytes)

def make_app():
    return tornado.web.Application([
        (r"/empty-gif", EmptyGifHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Starlette

Starlette is a lightweight ASGI framework/toolkit, which is ideal for building high performance asyncio services. It’s based on the Starlette ASGI toolkit, and is the underlying engine powering FastAPI.

from starlette.responses import Response

async def empty_gif(request):
    gif_bytes = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
    return Response(content=gif_bytes, media_type='image/gif')

app = Starlette(routes=[Route('/empty-gif', empty_gif)])

Quart

Quart is a Python ASGI web microframework based on Hypercorn and H11. It is a good choice for building high performance asyncio services.


from quart import Quart, Response

app = Quart(__name__)

@app.route("/empty-gif")
async def empty_gif():
    gif_bytes = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
    return Response(gif_bytes, mimetype='image/gif')

Sanic

Sanic is a Python 3.6+ web server and web framework that’s written to go fast. It allows the usage of the async/await syntax added in Python 3.5, which makes your code non-blocking and speedy.

from sanic import Sanic, response

app = Sanic(__name__)

@app.route("/empty-gif")
async def empty_gif(request):
    gif_bytes = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
    return response.raw(gif_bytes, content_type='image/gif')