Skip to content

Instantly share code, notes, and snippets.

@sayz
Created April 26, 2024 09:03
Show Gist options
  • Save sayz/162efd850781bcdb1cefe727987d493b to your computer and use it in GitHub Desktop.
Save sayz/162efd850781bcdb1cefe727987d493b to your computer and use it in GitHub Desktop.
import base64
from http.server import BaseHTTPRequestHandler, HTTPServer
# Base64 stringini JPEG dosyasına dönüştürme fonksiyonu
def base64_to_image(base64_string, image_path):
image_data = base64.b64decode(base64_string)
with open(image_path, 'wb') as file:
file.write(image_data)
# HTTP sunucusu için handler sınıfı
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
# İstemciye sunulacak JPEG dosyasının yolu
image_path = 'image.jpeg'
# Dosya içeriğini okuyup istemciye gönder
self.send_response(200)
self.send_header('Content-type', 'image/jpeg')
self.end_headers()
with open(image_path, 'rb') as file:
self.wfile.write(file.read())
# Base64 kodunu içeren dosyadan okuma
def read_base64_from_file(file_path):
with open(file_path, 'r') as file:
return file.read().strip()
# Dosyadan base64 stringi oku
base64_string = read_base64_from_file('test.txt')
# Base64 stringi JPEG'e çevir ve dosya olarak kaydet
image_path = 'image.jpeg'
base64_to_image(base64_string, image_path)
# HTTP sunucusunu başlat
port = 8000
server_address = ('', port)
httpd = HTTPServer(server_address, RequestHandler)
print(f"Server is running on port {port}")
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment