I am trying to come up with a python script to retrieve data from MySQL and post the data in json format to a web server. I have two separate python codes, one for retrieving the data in MySQL and one for posting the data in json format. The main issue that I am facing is that I do not know how to integrate them together.
Code for retrieving data from MySQL:
import MySQLdb
db = MySQLdb.connect("locahost", "root", "12345", "testdatabase")
curs=db.cursor()
curs.execute("SELECT * from mydata")
reading = curs.fetchall()
print "Data Info: %s" % reading
Code for posting to web server:
import json
import urllib2
import requests
data = {
'ID' :1
'Name' :Bryan
'Class' :3A
}
req = urllib2.Request('http://abcd.com') //not the actual url
req.add_header('Content type', 'application/json')
response=urllib.urlopen(req, json.dumps(data))
I have referenced the codes from the following links:
Retrieve data from MySQL 2nd link
Would appreciate any form of assistance.