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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:06:33+00:00 2026-06-09T21:06:33+00:00

I’m trying to find out how to create a local connection between a Python

  • 0

I’m trying to find out how to create a local connection between a Python server and a Javascript client using the JSON format for the data to be retrieved. Particularly, I need to make some queries on the HTML client side, send these queries to the server on JSON format and run them on the Python server side to search for data on a SQLite Database. And after getting the results from the database, send those results back to the client in JSON format too.

By now, I just can run the query on Python and code it on JSON like this:

import sqlite3 as dbapi
import json

connection = dbapi.connect("C:/folder/database.db")
mycursor = connection.cursor()
mycursor.execute("select * from people")
results = []
for information in mycursor.fetchall():
        results += information

onFormat = json.dumps(results)
print(onFormat)

I know this code does something alike (in fact it runs), because it calls a service on a server which returns data in JSON format (but the server in this example is NOT Python):

<html>
    <head>
        <style>img{ height: 100px; float: left; }</style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <div id="images"></div>
    <script>
      $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
      {
        tags: "mount rainier",
        tagmode: "any",
        format: "json"
      },
      function(data) {
        $.each(data.items, function(i,item){
          $("<img/>").attr("src", item.media.m).appendTo("#images");
          if ( i == 3 ) return false;
        });
      });</script>

    </body>
</html>

What I need is to know how should I run (locally) the python program to be an available running web-service and how should be the Javascript to retrieve the data from the python server.

I’ve looking for this on internet everywhere but I didn’t find this answer anywhere because the only answers they give are on how to code JSON inside Python or inside Javascript but not connecting both. Hope somebody can help me on this!!!

  • 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-09T21:06:35+00:00Added an answer on June 9, 2026 at 9:06 pm

    I found finally an easier way than Flask. It’s a Python framework called Bottle You only need to download the library from the official web site and put all its files in your working directory in order to import the library. You can also install it using the setup python program included to avoid carrying with the sourcecode everywhere. Then, for making your Web Service Server you can code it like this:

    from bottle import hook, response, route, run, static_file, request
    import json
    import socket
    import sqlite3
    
    #These lines are needed for avoiding the "Access-Control-Allow-Origin" errors
    @hook('after_request')
    def enable_cors():
        response.headers['Access-Control-Allow-Origin'] = '*'
    
    #Note that the text on the route decorator is the name of the resource
    # and the name of the function which answers the request could have any name
    @route('/examplePage')
    def exPage():
        return "<h1>This is an example of web page</h1><hr/><h2>Hope you enjoy it!</h2>"
    
    #If you want to return a JSON you can use a common dict of Python, 
    # the conversion to JSON is automatically done by the framework
    @route('/sampleJSON', method='GET')
    def mySample():
        return { "first": "This is the first", "second": "the second one here", "third": "and finally the third one!" }
    
    #If you have to send parameters, the right sintax is as calling the resoure
    # with a kind of path, with the parameters separed with slash ( / ) and they 
    # MUST to be written inside the lesser/greater than signs  ( <parameter_name> ) 
    @route('/dataQuery/<name>/<age>')
    def myQuery(name,age):
        connection= sqlite3.connect("C:/folder/data.db")
        mycursor = connection.cursor()
        mycursor.execute("select * from client where name = ? and age= ?",(name, age))
        results = mycursor.fetchall()
        theQuery = []
        for tuple in results:
            theQuery.append({"name":tuple[0],"age":tuple[1]})
        return json.dumps(theQuery)
    
    #If you want to send images in jpg format you can use this below
    @route('/images/<filename:re:.*\.jpg>')
    def send_image(filename):
        return static_file(filename, root="C:/folder/images", mimetype="image/jpg")
    
    #To send a favicon to a webpage use this below
    @route('/favicon.ico')
    def favicon():
        return static_file('windowIcon.ico', root="C:/folder/images", mimetype="image/ico")
    
    #And the MOST important line to set this program as a web service provider is this
    run(host=socket.gethostname(), port=8000)
    

    Finally, you can call the REST web service of your Bottlepy app on a Javascript client in this way:

    var addr = "192.168.1.100"
    var port = "8000"
    
    function makeQuery(name, age){
        jQuery.get("http://"+addr+":"+port+"/dataQuery/"+ name+ "/" + age, function(result){
            myRes = jQuery.parseJSON(result);
            toStore= "<table border='2' bordercolor='#397056'><tr><td><strong>name</strong></td><td><strong>age</strong></td></tr>";
            $.each(myRes, function(i, element){
                toStore= toStore+ "<tr><td>"+element.name+"</td><td>" + element.age+ "</td></td></tr>";
            })
            toStore= toStore+ "</table>"
            $('#theDataDiv').text('');
            $('<br/>').appendTo('#theDataDiv');
            $(toStore).appendTo('#theDataDiv');
            $('<br/>').appendTo('#theDataDiv');
        })
    }
    

    I hope it could be useful for somebody else

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small

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.