0

I am building a simple server which responds to a GET request and sends html which contains table of utf-8 values. The table is created from json object by using pandas DataFrame and to_html method

The server is based on BaseHTTPRequestHandler class (http.server module)

When I send a GET request from the Chrome browser, I receive gibberish text values

I tried to add the charset tag to the header using either

self.send_header('Content-type', 'text/html; charset=utf-8')
or
self.send_header('charset','utf-8')

table

My code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import json
import requests
from http.server import HTTPServer, BaseHTTPRequestHandler
from sys import argv

BIND_HOST = 'localhost'
PORT_HOST = 8000

input = {0: {'יעד': 'באר שבע מרכז', 'זמן הגעה': '17:38:00', 'רציף': '1'}, 1: {'יעד': 'ראש העין צפון', 'זמן הגעה': '17:48:00', 'רציף': '2'}}

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

     def do_GET(self):
        try:
            output = pd.DataFrame(input).transpose().to_html()
            print(output)
            self.write_response(bytes(output))
        except Exception as e:
            print(e)

     def write_response(self, content):
 #      self.send_header('Content-type', 'text/html; charset=utf-8')
 #      self.send_header('charset','utf-8')
        self.send_response(200)
        self.end_headers()
        print(content.encode('utf-8'))
        self.wfile.write(content.encode(encoding='utf-8'))


if len(argv) > 1:
    arg = argv[1].split(':')
    BIND_HOST = arg[0]
    PORT_HOST = int(arg[1])

httpd = HTTPServer((BIND_HOST, PORT_HOST), SimpleHTTPRequestHandler)
print(f'Listening on http://{BIND_HOST}:{PORT_HOST}\n')
httpd.serve_forever()

       
2
  • 2
    What is pd in your code? Could you post a minimal reproducible example we can all run? Commented Dec 15, 2020 at 23:05
  • pd for pandas, adding my code in the original question Commented Dec 16, 2020 at 7:17

1 Answer 1

0

You need to send the content-type header.

# -*- coding: utf-8 -*-
import pandas as pd
import json
import requests
from http.server import HTTPServer, BaseHTTPRequestHandler
from sys import argv

BIND_HOST = 'localhost'
PORT_HOST = 8000

input = {0: {'יעד': 'באר שבע מרכז', 'זמן הגעה': '17:38:00', 'רציף': '1'}, 1: {'יעד': 'ראש העין צפון', 'זמן הגעה': '17:48:00', 'רציף': '2'}}

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

     def do_GET(self):
        output = pd.DataFrame(input).transpose().to_html()
        self.write_response(output.encode('utf-8'))

     def write_response(self, content):
        self.send_response(200)
        self.send_header('Content-type', 'text/html; charset=utf-8')
        self.end_headers()
        self.wfile.write(content)


if len(argv) > 1:
    arg = argv[1].split(':')
    BIND_HOST = arg[0]
    PORT_HOST = int(arg[1])

httpd = HTTPServer((BIND_HOST, PORT_HOST), SimpleHTTPRequestHandler)
print(f'Listening on http://{BIND_HOST}:{PORT_HOST}\n')
httpd.serve_forever()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.