python - Pyscopg DB - Error Adding Persistence to code -
i working on online project udacity. using vagrant configured them, run server containing database. unfortunately when tried give code persistence, server returns error everytime. new python please forgive obvious mistakes.
here 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 int() 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 code have changed in forumdb.py :
# # database access functions web forum. # import psycopg2 ## database connection def getallposts(): db = psycopg2.connect("dbname=forum") c = db.cursor() c.execute("select time, content posts order time desc") posts = ({'content': str(row[1]), 'time': str(row[0])} row in c.fetchall()) # returns dictionary -- returning c.fetchall() return list of tuples db.close() return posts def addpost(content): db = psycopg2.connect("dbname=forum") c = db.cursor() c.execute("insert posts (content) values ('%s')" % content) db.commit() db.close() forum.py - file renders html bringing data db : http://pastebin.com/zihwiiwr
please !
you're querying wsgi environment length = int(env.get('content_length', 0)) (forum.py:68). ran sample wsgi server (example code taken python docs), outputs available environment-variables upon request:
from wsgiref.util import setup_testing_defaults wsgiref.simple_server import make_server # relatively simple wsgi application. it's going print out # environment dictionary after being updated 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) key, value in environ.iteritems()] return ret httpd = make_server('', 8000, simple_app) print "serving on port 8000..." httpd.serve_forever() the output i'm getting when querying test-server (among lot of other variables):
server_port: 8000 content_length: glade_catalog_path: : you see content_length variable empty. seems case in application well.
if env-dictionary queried env.get('content_length', 0), content_length-key found, it's value empty string - that's why get() method returns '' , not specified default value 0.
since empty string can't converted int, you're getting valueerror.
try catching exception , code should work:
try: length = int(env.get("content_length", 0)) except valueerror: length = 0
Comments
Post a Comment