Pythons BaseHTTPServer returns junky responses

2024/10/10 23:17:24

I use Python's BaseHTTPServer and implement the following very simple BaseHTTPRequestHandler:

class WorkerHandler(BaseHTTPRequestHandler):def do_GET(self):self.wfile.write('{"status" : "ready"}')self.send_response(200)

When I run a GET query from the web browser, by simply going to localhost:port, I get the following response:

{"status" : "ready"}HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.7.12
Date: Mon, 09 Jan 2017 12:45:13 GMT

I only want the JSON. How can I make the server not sending this junky data?

HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.7.12
Date: Mon, 09 Jan 2017 12:45:13 GMT
Answer

Finally succeeded fixing it myself. Sharing with you:

class WorkerHandler(BaseHTTPRequestHandler):def do_GET(self):self.send_response(200)self.end_headers()self.wfile.write('{"status" : "ready"}')

Swapped the send_response and wfile.write. Also added end_headers after send_response

https://en.xdnf.cn/q/118396.html

Related Q&A

Why is matplotlib failing on import matplotlib.pyplot as plt

I installed matplotlib using conda:conda install matplotlibThe following code failed:#!/usr/bin/env python import matplotlib import matplotlib.pyplot as pltWith this error message:"ImportError: N…

Setting cell color of matplotlib table and save as a figure?

Im following this code a link! to save a table as the image, and I have some feature like check value in a cell then set color for a cell, but I added some code stylemap, it doesnt workimport pandas a…

Errno 111 Connection refused - Python Mininet API hosts client/server no connection?

I am new to Mininet and I am trying to find a way to use a script in python to execute a few tests using Mininet. More precisely I want to build topology and send a few xmlrpc request from one host t…

Finding the Corners of the an array of coordinates

I have a 2D array of Coordinates in Numpy.My goal is to attempt to find the corners (as if it were a square). So the :Top left: smallest x, highest y Top right: largest x, largest y bottom left: smalle…

How to make a dictionary retain its sort order?

def positive(self):total = {}final = {}for word in envir:for i in self.lst:if word in i:if word in total:total[word] += 1else:total[word] = 1final = sorted(total, reverse = True)return totalThis return…

Counting line frequencies and producing output files

With a textfile like this:a;b b;a c;d d;c e;a f;g h;b b;f b;f c;g a;b d;fHow can one read it, and produce two output text files: one keeping only the lines representing the most often occurring couple …

Check if parent dict is not empty and retrieve the value of the nested dict

Lets suppose that I have a nested dictionary which looks like that:parent_dict = { parent_key: {child_key: child_value}How can I write the following code:if parent_dict.get(parent_key) is not None and …

List combinations in defined range

I am writing parallel rainbow tables generator using parallel python and multiple machines. So far, I have it working on a single machine. It creates all possible passwords, hashes them, saves to file.…

Python turtle drawing a symbol

import turtlewin=turtle.Screen()t = turtle.Turtle() t.width(5)#The vertical and horizontal lines t.left(90) t.forward(70) t.left(90) t.forward(20)t.left(90) t.forward(60) t.left(120) t.forward(35) t.b…

Display a countdown for the python sleep function in discord embed in python

hi all I am doing one discord bot I need to send one countdown its like a cooldown embed after every request I did this code but I dont know how to add this in my embedfor i in range(60,0,-1):print(f&q…