Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95520

Re: SQLite3 and web server

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Re: SQLite3 and web server
Date 2015-08-21 11:44 +0200
Organization Decebal Computing
Message-ID <87h9ntou1c.fsf@Equus.decebal.nl> (permalink)
References <87pp2hp3vg.fsf@Equus.decebal.nl> <55d6ca35$0$1665$c3e8da3$5496439d@news.astraweb.com> <87lhd5p0zv.fsf@Equus.decebal.nl> <mailman.6.1440142039.13558.python-list@python.org>

Show all headers | View raw


On Friday 21 Aug 2015 09:27 CEST, Chris Angelico wrote:

> On Fri, Aug 21, 2015 at 5:13 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>> I know how to work with SQLite. What I do not know how to make a
>> python web-server that accepts a request from the JavaScript code
>> and responds with data from the SQLite3 database.
>
> The request from JS will be some sort of HTTP transaction. It'll be
> AJAX or similar, and to your web server, it'll simply be another
> page request. (You might be using a method other than GET/POST
> (which are the only two that regular page loads use), but it's still
> a standard HTTP request.) You then respond to that request with data
> that you got from the database.
>
> Check out this video from PyCon. Its main thrust is "here's why
> websockets are great, and why other technologies are insufficient",
> but the background info is a tidy summary of how the web works.
>
> https://www.youtube.com/watch?v=u5QT3luWx7w

Interesting, but it does not help me. I need to know how to server
data from SQLite instead of static files.

I understood that CGI is old school and should not be used anymore,
but I tried it anyway. (Because that is the only thing I found.)

I use the following script:
========================================================================
#!/usr/bin/env python3

import http.server
import os
import socketserver
import sys

os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))

PORT = 8000
Handler = http.server.CGIHTTPRequestHandler
httpd   = socketserver.TCPServer(('', PORT), Handler)
print('serving at port {0}'.format(PORT))
httpd.serve_forever()
========================================================================

In cgi-bin I have the script helloTxt.py, which contains:
========================================================================
#!/usr/bin/env python3

print('Content-type: text/plain\n\n')
print ('Hello World!\n')
========================================================================

My static files are handled correctly, but when I enter:
    localhost:8000/cgi-bin/helloTxt.py
I get:
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49380)
Traceback (most recent call last):
  File "/usr/lib64/python3.4/socketserver.py", line 305, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib64/python3.4/socketserver.py", line 331, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib64/python3.4/socketserver.py", line 344, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib64/python3.4/socketserver.py", line 665, in __init__
    self.handle()
  File "/usr/lib64/python3.4/http/server.py", line 398, in handle
    self.handle_one_request()
  File "/usr/lib64/python3.4/http/server.py", line 386, in handle_one_request
    method()
  File "/usr/lib64/python3.4/http/server.py", line 677, in do_GET
    f = self.send_head()
  File "/usr/lib64/python3.4/http/server.py", line 961, in send_head
    return self.run_cgi()
  File "/usr/lib64/python3.4/http/server.py", line 1051, in run_cgi
    env['SERVER_NAME'] = self.server.server_name
AttributeError: 'TCPServer' object has no attribute 'server_name'
----------------------------------------


-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 08:11 +0200
  Re: SQLite3 and web server Steven D'Aprano <steve@pearwood.info> - 2015-08-21 16:50 +1000
    Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 09:13 +0200
      Re: SQLite3 and web server Chris Angelico <rosuav@gmail.com> - 2015-08-21 17:27 +1000
        Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 11:44 +0200
          Re: SQLite3 and web server Michael Torrie <torriem@gmail.com> - 2015-08-21 08:19 -0600
            Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 17:23 +0200
          Re: SQLite3 and web server Michael Torrie <torriem@gmail.com> - 2015-08-21 08:57 -0600
            Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-22 13:38 +0200
  Re: SQLite3 and web server Peter Otten <__peter__@web.de> - 2015-08-21 11:32 +0200
    Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 17:12 +0200
      Re: SQLite3 and web server Peter Otten <__peter__@web.de> - 2015-08-22 09:51 +0200
        Re: SQLite3 and web server Cecil Westerhof <Cecil@decebal.nl> - 2015-08-22 13:41 +0200
          Re: SQLite3 and web server Peter Otten <__peter__@web.de> - 2015-08-22 14:23 +0200

csiph-web