Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:28:09+00:00 2026-05-24T06:28:09+00:00

I’m writing my own WSGI Simple Server. I want to be able to send

  • 0

I’m writing my own WSGI Simple Server. I want to be able to send a few requests to it and have some responses returned.

I have a request that comes in from fiddler, gets processed and returned to fiddler. I’m currently ONLY trying to return a success code… Fiddler gets back a 200 success/OK response, which is great. But then when I look at python it’s thrown an assertion failure! Help! How do I avoid the assertion!?

My call stack ends up going

--> socketserver.py - handle_request_noblock
--> socketserver.py - process_request
--> socketserver.py - finish_request
--> simpleserver.py - handle
--> handlers.py - run
--> handlers.py - finish_response
--> handlers.py - write

The first few lines in the definition of write() are…

def write(self, data):
    """'write()' callable as specified by PEP 3333"""

    assert type(data) is bytes, \
        "write() argument must be a bytes instance"

At this point data has a type which is “str”. Surely write should attempt to convert to bytes before throwing this exception?!? I’m truly exasperated by this!

Could anyone throw any light on what I’m doing wrong!??!

// Response class based on Google WebAppEngine, Which is NOT written in Python 3
class Response(object):
.. Omitted .. (See Google AppEngine?!)
def wsgi_write(self, start_response):
    body = self.out.getvalue()
    if isinstance(body, str):
            body = body.encode('utf-8')
    elif self.headers.get('Content-Type', '').endswith('; charset=utf-8'):
            try:
                    body.decode('utf-8')
            except UnicodeError as e:

                    print('Response written is not UTF-8: %s', e)

    if (self.headers.get('Cache-Control') == 'no-cache' and not self.headers.get('Expires')):
            self.headers['Expires'] = 'Fri, 01 Jan 1990 00:00:00 GMT'
    self.headers['Content-Length'] = str(len(body))

    new_headers = []
    for header, value in self.__wsgi_headers:
            if not isinstance(value, str):
                    value = str(value)
            if ('\n' in header or '\r' in header or '\n' in value or '\r' in value):
                    value = value.replace('\n','').replace('\r','')
                    header = header.replace('\n','').replace('\r','')
            new_headers.append((header, value))

    self.__wsgi_headers = new_headers

    write = start_response('%d %s' % (self.__status, self.__wsgi_headers))
    write(bytes(body))
    self.out.close()

class WSGIApplication(object):
.. Omitted..

class Test(RequestHandler):
    def post(self):
        if self.response == None:
            print("No response")
        self.response.out.write("Success!")
        self.response.set_status(200, 'OK')

if __name__ == '__main__':
    print("Starting application")
    application = WSGIApplication([('/test', Test)])
    server = wsgiref.simple_server.make_server('', 109, application)
    server.serve_forever()

The only thing I can think that it would be is the wsgi_write response from the google app engine Response object, which I’ve borrowed and manipulated for my own purposes. I’m really not sure of the unicode stuff in the response. I’d appreciate any pointers… I did try to convert it but I’ve obviously got some stuff wrong?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:28:09+00:00Added an answer on May 24, 2026 at 6:28 am

    Surely write should attempt to convert to bytes before throwing this exception?!?

    No, the ones who use your server should convert to bytes before calling write(), using their favorite encoding (which they also should specify in a header).

    (That said, they probably shouldn’t use write at all, see PEP3333.)

    Could anyone throw any light on what I’m doing wrong!??!

    Yes, as the errors tells you: The client should send in a bytes instance, not a str instance (or better, return an iterable instead of using write()). I’m a bit mystified by your code, where you apparently convert a string to bytes before writing it, except if it already is bytes, in which case you convert it to a string!?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am writing an app with both english and french support. The app requests
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.