The custom wsgi function in python3 reports an error in the make_server function.

Source: Internet
Author: User

The custom wsgi function in python3 reports an error in the make_server function.

# Coding: UTF-8

From wsgiref. simple_server import make_server

Def RunServer (environ, start_response ):
Start_response ('2017 OK ', [('content-type', 'text/html')])
Return '
If _ name _ = '_ main __':
Httpd = make_server ('localhost', 8000, RunServer)
Print ("Serving HTTP on port 8000 ...")
Httpd. serve_forever ()
This code can be run in python2.7 and run in python3.4. The error is reported as follows:

Serving HTTP on port 8000...
127.0.0.1--[12/Apr/2016 16:44:17] "GET/HTTP/1.1" 200 0
Traceback (most recent call last ):
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 138, in run
Self. finish_response ()
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 181, in finish_response
Self. write (data)
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 267, in write
"Write () argument must be a bytes instance"
AssertionError: write () argument must be a bytes instance
127.0.0.1--[12/Apr/2016 16:44:17] "GET/HTTP/1.1" 500 59
Traceback (most recent call last ):
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 138, in run
Self. finish_response ()
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 181, in finish_response
Self. write (data)
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 267, in write
"Write () argument must be a bytes instance"
AssertionError: write () argument must be a bytes instance

During handling of the above exception, another exception occurred:

Traceback (most recent call last ):
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 141, in run
Self. handle_error ()
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 369, in handle_error
Self. finish_response ()
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 181, in finish_response
Self. write (data)
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 275, in write
Self. send_headers ()
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 332, in send_headers
If not self. origin_server or self. client_is_modern ():
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 345, in client_is_modern
Return self. environ ['server _ Protocol']. upper ()! = 'HTTP/0.9'
TypeError: 'nonetype 'object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last ):
----------------------------------------
Exception happened during processing of request from ('2017. 0.0.1 ', 127)
File "C: \ Python34 \ lib \ socketserver. py", line 305, in _ handle_request_noblock
Self. process_request (request, client_address)
File "C: \ Python34 \ lib \ socketserver. py", line 331, in process_request
Self. finish_request (request, client_address)
File "C: \ Python34 \ lib \ socketserver. py", line 344, in finish_request
Self. RequestHandlerClass (request, client_address, self)
File "C: \ Python34 \ lib \ socketserver. py", line 673, in _ init __
Self. handle ()
File "C: \ Python34 \ lib \ wsgiref \ simple_server.py", line 133, in handle
Handler. run (self. server. get_app ())
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 144, in run
Self. close ()
File "C: \ Python34 \ lib \ wsgiref \ simple_server.py", line 35, in close
Self. status. split ('', 1) [0], self. bytes_sent
AttributeError: 'nonetype 'object has no attribute 'split'
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('2017. 0.0.1 ', 127)
----------------------------------------
127.0.0.1--[12/Apr/2016 16:44:18] "GET/HTTP/1.1" 200 0
Traceback (most recent call last ):
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 138, in run
Self. finish_response ()
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 181, in finish_response
Self. write (data)
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 267, in write
"Write () argument must be a bytes instance"
AssertionError: write () argument must be a bytes instance
127.0.0.1--[12/Apr/2016 16:44:18] "GET/HTTP/1.1" 500 59
Traceback (most recent call last ):
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 138, in run
Self. finish_response ()
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 181, in finish_response
Self. write (data)
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 267, in write
"Write () argument must be a bytes instance"
AssertionError: write () argument must be a bytes instance

During handling of the above exception, another exception occurred:

Traceback (most recent call last ):
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 141, in run
Self. handle_error ()
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 369, in handle_error
Self. finish_response ()
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 181, in finish_response
Self. write (data)
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 275, in write
Self. send_headers ()
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 332, in send_headers
If not self. origin_server or self. client_is_modern ():
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 345, in client_is_modern
Return self. environ ['server _ Protocol']. upper ()! = 'HTTP/0.9'
TypeError: 'nonetype 'object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last ):
File "C: \ Python34 \ lib \ socketserver. py", line 305, in _ handle_request_noblock
Self. process_request (request, client_address)
File "C: \ Python34 \ lib \ socketserver. py", line 331, in process_request
Self. finish_request (request, client_address)
File "C: \ Python34 \ lib \ socketserver. py", line 344, in finish_request
Self. RequestHandlerClass (request, client_address, self)
File "C: \ Python34 \ lib \ socketserver. py", line 673, in _ init __
Self. handle ()
File "C: \ Python34 \ lib \ wsgiref \ simple_server.py", line 133, in handle
Handler. run (self. server. get_app ())
File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 144, in run
Self. close ()
File "C: \ Python34 \ lib \ wsgiref \ simple_server.py", line 35, in close
Self. status. split ('', 1) [0], self. bytes_sent
AttributeError: 'nonetype 'object has no attribute 'split'

Suddenly, with so many errors reported, I got a glimpse of it. Various google Baidu and various queries can be found by google at that time,

However, the English language is not good and I cannot quite understand it. Baidu finds all the junk information, which is useless at all. In the end, I tried to read the source code at 1.1 o'clock.

First, follow the prompts In the first line:

File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 138, in run
Self. finish_response ()

The editor I use here is pycharm. Find the 138 line of the handlers. py file, and press ctrl to click the finish_response () method in the file,

Find the position defined by self. finish_response. Follow the second prompt:

File "C: \ Python34 \ lib \ wsgiref \ handlers. py", line 181, in finish_response
Self. write (data)

If an error is found in the write method, press ctrl and click the write method to locate the position for defining the write method. The first line defines an error:

 

Assert type (data) is bytes ,\
"Write () argument must be a bytes instance"
According to the preceding error message, it is found that the data type of the variable is not bytes. Therefore, add a sentence on the handlers. py181 line code self. write (data:
Data = data. encode (), refresh the program again, and find that all the errors are missing and the program runs properly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.