Pyscopg DB - Error adding save to code

I am working on an online project using Udacity . I use the vagrant configured by them to start the server containing the database. Unfortunately, when I tried to ensure code consistency, the server returns an error every time. I am new to python, so please forgive any obvious errors.

Here is the error:

 Serving HTTP on port 8000... Traceback (most recent call last): File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run self.result = application(self.environ, self.start_response) File "forum.py", line 95, in Dispatcher return DISPATCH[page](env, resp) File "forum.py", line 68, in Post length = int(env.get('CONTENT_LENGTH', 0)) ValueError: invalid literal for int() with base 10: '' 10.0.2.2 - - [06/Jan/2016 04:44:16] "GET /post HTTP/1.1" 500 59 10.0.2.2 - - [06/Jan/2016 04:44:16] "GET /favicon.ico HTTP/1.1" 404 22 

And this is the code I changed in forumdb.py :

 # # Database access functions for the web forum. # import psycopg2 ## Database connection def GetAllPosts(): DB = psycopg2.connect("dbname=forum") c = DB.cursor() c.execute("SELECT time, content FROM posts ORDER BY time DESC") posts = ({'content': str(row[1]), 'time': str(row[0])} for row in c.fetchall()) # This returns a dictionary -- returning just c.fetchall() will return a list of tuples DB.close() return posts def AddPost(content): DB = psycopg2.connect("dbname=forum") c = DB.cursor() c.execute("INSERT INTO posts (content) values ('%s')" % content) DB.commit() DB.close() 

forum.py - this file displays html outputting data from the database: http://pastebin.com/ZiHWiiwr

Please, help!

+5
source share
2 answers

You are requesting a WSGI environment using length = int(env.get('CONTENT_LENGTH', 0)) (forum.py:68). I just ran an example WSGI server (sample code taken from python docs) that returns all available environment variables on request:

 from wsgiref.util import setup_testing_defaults from wsgiref.simple_server import make_server # A relatively simple WSGI application. It going to print out the # environment dictionary after being updated by setup_testing_defaults def simple_app(environ, start_response): setup_testing_defaults(environ) status = '200 OK' headers = [('Content-type', 'text/plain')] start_response(status, headers) ret = ["%s: %s\n" % (key, value) for key, value in environ.iteritems()] return ret httpd = make_server('', 8000, simple_app) print "Serving on port 8000..." httpd.serve_forever() 

The output that I get when I request a test server (among many other variables):

 SERVER_PORT: 8000 CONTENT_LENGTH: GLADE_CATALOG_PATH: : 

You see that the variable CONTENT_LENGTH is empty. This is similar to your application.

If now env-dictionary is requested using env.get('CONTENT_LENGTH', 0) , CONTENT_LENGTH-key , but this value is an empty string, so the get () method returns '' and the default value of 0 is not specified by you.

Since an empty string cannot be converted to int, you get a ValueError value.

Try to catch the exception and your code should work:

 try: length = int(env.get("CONTENT_LENGTH", 0)) except ValueError: length = 0 
+2
source

Your current error is due to a line

 length = int(env.get('CONTENT_LENGTH', 0)) 

in the forum.py file. Basically, the key is CONTENT_LENGTH , and this is an empty string, and the empty string cannot be converted to int. Change this line to

 length = int(env.get('CONTENT_LENGTH')) if env.get('CONTENT_LENGTH') else 0 

Since you're new to Python, there are a couple things you should know about a modified string. This is first known as a conditional expression, the second empty lines in Python have the boolean value False, so when

  • env.get ('CONTENT_LENGTH') returns an empty string, then a length of 0 is assigned
  • env.get ('CONTENT_LENGTH') returns a non-empty string or integer, then int convert this value to an integer representation
  • env.get ('CONTENT_LENGTH') returns 0 (which has a boolean false), then 0 is assigned
+2
source

Source: https://habr.com/ru/post/1239959/


All Articles