Python receive data from PHP page with MYSQL

Python receive data from PHP page with MYSQL

Postby nl2ttl » Sun Sep 01, 2013 8:17 pm

I have a website running with users and infromation about them.

PHP + MYSQL

I can not connect directly to the MYSQL from external locations. (Server in datacenter / python script on mobile device)

What I need to do is the following:

1. Get username and information from the database using the Python script.
2. Send data back to the MYSQL

The second part can be done by opening a page like: http://testserver.com/data.php?user_id=111&value=250

Only before I can send the user id i have to get it in the python script. And at this part i'm getting lost.
nl2ttl
 
Posts: 10
Joined: Fri May 17, 2013 1:44 pm

Re: Python receive data from PHP page with MYSQL

Postby micseydel » Mon Sep 02, 2013 3:12 am

I'm really not sure what your question is here. You're not sure how to get the user ID into the Python script?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1506
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Python receive data from PHP page with MYSQL

Postby nl2ttl » Mon Sep 02, 2013 6:25 am

That's right. How to receive the username from the database.

Python sends from external location a RFID number to the website, I like to receive te Username.
nl2ttl
 
Posts: 10
Joined: Fri May 17, 2013 1:44 pm

Re: Python receive data from PHP page with MYSQL

Postby stranac » Mon Sep 02, 2013 2:35 pm

Create a file, 'username.php', which will take the id as a parameter, and return the username from the db.
Then, you open the website( http://testserver.com/username.php?user_id=111) from python and receive the username.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python receive data from PHP page with MYSQL

Postby nl2ttl » Mon Sep 02, 2013 3:31 pm

Only how do I receive the response. Thats the real question.

With the url http://testserver.com/username.php?user_id=111) I do a search in the database and this give the result: "NL2TTL" this result I want to get in the python script.
nl2ttl
 
Posts: 10
Joined: Fri May 17, 2013 1:44 pm

Re: Python receive data from PHP page with MYSQL

Postby stranac » Mon Sep 02, 2013 4:09 pm

Use urllib.urlopen() or requests.get() or something like that to make the request to the website, an then simply read the response.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python receive data from PHP page with MYSQL

Postby nl2ttl » Mon Sep 02, 2013 5:46 pm

I have created a webpage that checks for userid

Code: Select all
<?php
   $userid = $_GET["userid"];
   
   if ($userid == "1")
    {
        echo "Gebruiker1";
      $username = "Player1";
     }
   elseif ($userid == "2")
     {
        echo "Gebruiker2";
      $username = "Player2";
    }
   else
     {
        echo "Onbekende userid";
   }   
?>


In python i have this:

Code: Select all
import httplib2
resp, content = httplib2.Http().request("http://127.0.0.1/fcc/username.php?userid=1")
print resp


When I execute the command i get the complete webpage response back. Is it possible to only het the value of $username. Now I get the following back:

Code: Select all
python_test>c:\Python27\python fcc.py
tatus': '200', 'content-length': '328', 'content-location': 'http://127.0.0.1/fcc/username.php?userid=1', 'x-powered-by': 'PHP/5.4.7', 'server': 'A
he/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7', 'date': 'Mon, 02 Sep 2013 17:42:19 GMT', 'content-type': 'text/html'}

python_test>
nl2ttl
 
Posts: 10
Joined: Fri May 17, 2013 1:44 pm

Re: Python receive data from PHP page with MYSQL

Postby stranac » Mon Sep 02, 2013 5:54 pm

The output of your php script will have to contain the username for your python script to be able to get it.
So you'll have to echo the $username variable.

Also, I never used httplib2, but I would guess that the page's content will be in content...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python receive data from PHP page with MYSQL

Postby nl2ttl » Mon Sep 02, 2013 8:18 pm

Code: Select all
import httplib2
resp, content = httplib2.Http().request("http://127.0.0.1/fcc/userinfo.php?rfidtag=11111111")
print content


This code does the job.

The only thin is how to filter the content string with data for serval parts of seperate data:

conten = <BART><SIMPSON>

So I like to put the tekst BART in a var $firstname and SIMPSON in the var $lastname

How can I check for the < and > to filter the length of the first and last name.
nl2ttl
 
Posts: 10
Joined: Fri May 17, 2013 1:44 pm

Re: Python receive data from PHP page with MYSQL

Postby stranac » Tue Sep 03, 2013 1:50 pm

Your explanation makes very little sense, so I'll just try guessing what i is you want.
Does this help?
Code: Select all
>>> content = '<BART><SIMPSON>'
>>> first, last = content.strip('><').split('><')
>>> first
'BART'
>>> last
'SIMPSON'
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm


Return to Networking

Who is online

Users browsing this forum: No registered users and 1 guest

cron