emptygif
மொழி: தமிழ்

அனைத்து மொழிகளும் ஃப்ரேம்வொர்க்குகளும்

Python standard library (http.server) Python

/pixel.gif க்கு வரும் ரெக்வெஸ்ட்களுக்கு empty GIF மூலம் பதிலளிக்கும், Python standard library (http.server) க்கான Python ஸ்னிப்பெட். GIF ஸ்டார்ட்அப்பில் ஒருமுறை மட்டுமே டீகோட் செய்யப்படுகிறது.

Python standard library (http.server) Python

ஃபைல்
server.py
ரன்
python3 server.py
import base64
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import urlsplit

GIF = base64.b64decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")


class PixelHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if urlsplit(self.path).path != "/pixel.gif":
            self.send_error(404)
            return
        self.send_response(200)
        self.send_header("Content-Type", "image/gif")
        self.send_header("Content-Length", str(len(GIF)))
        self.send_header("Cache-Control", "no-store")
        self.end_headers()
        self.wfile.write(GIF)


ThreadingHTTPServer(("", 8080), PixelHandler).serve_forever()

இந்த ஸ்னிப்பெட்டை ரன் செய்து, அது திருப்பித் தந்த பைட்களை ஒப்பிட்டுப் பார்த்தோம்.

சரிபார்

curl -sI http://localhost:8080/pixel.gif

அதிகாரப்பூர்வ சைட்: Python standard library (http.server)

Python இல் மேலும்