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 8764727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:06:26+00:00 2026-06-13T16:06:26+00:00

I have a pretty simple python webserver that returns a few web pages, and

  • 0

I have a pretty simple python webserver that returns a few web pages, and it keeps throwing TypeError: 'str' does not support the buffer interface. Here is my code, can anyone tell what is wrong?

from os import curdir
from os.path import join as pjoin
from http.server import BaseHTTPRequestHandler, HTTPServer
class StoreHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/store.json":
            with open(pjoin(curdir, 'store.json')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/json')
                self.end_headers()
                self.wfile.write(fh.read())
        elif self.path == "/Stock.htm":
            with open(pjoin(curdir, 'stock.htm')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/html')
                self.end_headers()
                self.wfile.write(fh.read())
        else:
            with open(pjoin(curdir, 'index.htm')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/html')
                self.end_headers()
                self.wfile.write(fh.read())
    def do_POST(self):
        if self.path == '/store.json':
            length = self.headers.getheader('content-length')
            data = self.rfile.read(int(length))
            with open(pjoin(curdir, 'store.json'), 'w') as fh:
                fh.write(data)
            self.send_response(200)

server = HTTPServer(('', 8080), StoreHandler)
server.serve_forever()

Here is the exception output:

127.0.0.1 - - [30/Oct/2012 16:48:17] "GET / HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 58645)
Traceback (most recent call last):
  File "C:\Program Files\Python33\lib\socketserver.py", line 306, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Program Files\Python33\lib\socketserver.py", line 332, in process_request
    self.finish_request(request, client_address)
  File "C:\Program Files\Python33\lib\socketserver.py", line 345, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files\Python33\lib\socketserver.py", line 666, in __init__
    self.handle()
  File "C:\Program Files\Python33\lib\http\server.py", line 400, in handle
    self.handle_one_request()
  File "C:\Program Files\Python33\lib\http\server.py", line 388, in handle_one_request
    method()
  File "C:\Users\Arlen\Desktop\Stock Recorder\webserver.py", line 25, in do_GET
    self.wfile.write(fh.read())
  File "C:\Program Files\Python33\lib\socket.py", line 317, in write
    return self._sock.send(b)
TypeError: 'str' does not support the buffer interface
----------------------------------------

Update: Here is how my updated code looks:

from os import curdir
from os.path import join as pjoin

from http.server import BaseHTTPRequestHandler, HTTPServer

class StoreHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/store.json":
            with open(pjoin(curdir, 'store.json')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/json')
                self.end_headers()
                self.wfile.write(fh.read(), 'rb')
        elif self.path == "/Stock.htm":
            with open(pjoin(curdir, 'stock.htm')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/html')
                self.end_headers()
                self.wfile.write(fh.read(), 'rb')
        else:
            with open(pjoin(curdir, 'index.htm')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/html')
                self.end_headers()
                self.wfile.write(fh.read(),'rb')
    def do_POST(self):
        if self.path == '/store.json':
            length = self.headers.getheader('content-length')
            data = self.rfile.read(int(length))
            with open(pjoin(curdir, 'store.json'), 'w') as fh:
                fh.write(data)
            self.send_response(200)

server = HTTPServer(('', 8080), StoreHandler)
server.serve_forever()
  • 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-06-13T16:06:27+00:00Added an answer on June 13, 2026 at 4:06 pm

    Sockets send and receive bytes, but you are attempting to send over unicode strings since you opened the file without specifying the mode (remember, in Python 3 all strings are unicode by default).

    You can either:

    • Use the bytes built-in function to transform the data

    – or –

    • Open the file in binary mode – change open(pjoin(curdir, 'a.file')) to open(pjoin(curdir, 'store.json'), 'rb') (note the additional rb parameter).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty simple ASP.NET Web Form that looks a bit like the
Pretty simple. I have a Python list that I am passing to a Django
I have a python question that I'm sure is pretty simple - please feel
I have a pretty simple python script that sends emails, something like this: import
I have a pretty simple trigger: CREATE OR REPLACE FUNCTION f_log_datei() RETURNS TRIGGER AS
I have a pretty simple problem. Basically I have an array called $list that
I have a simple Python script that asks for your name, then spits it
My question is pretty simple: If you have two web-application components: Server-side (secret-capable) code
I've pretty much tried every Python web framework that exists, and it took me
I'm a beginner in Python. My problem is pretty simple. I have a string

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.