0

I'm a bit lost at the moment and I hope that you can help!

I try to recognize people with OpenCV in Python. This is working so far. With the id of the recognized person I want to display information which belong to her or him.

For that I tried something like this:

1)

import webbrowser
webbrowser.open('http://mysite/index.php?userID=XYZ', new = 0)

But although I wrote the "new = 0" it opens a new browser tab everytime an other userID stands in the link.

2) An other approach was to send information via a http post request to the website like:

url = 'http://mysite/index.php'
query = {'personID': XYZ}
res = requests.post(url, data=query)

But doing this I don't have any idea how to work with this post command in my PHP code so it refreshes the site with the data belonging to user XYZ..

May you help me please?

Kind regards


Edit1 - START: A small php example with using GET parameters would be something like this

<?php
    echo("<html><head><title></title></head><body>");

    if ($_GET['userID'] == 1) {
        echo("<div id='userContent'>Hello User 1</div>");
    }   
    else if ($_GET['userID'] == 2) {
        echo("<div id='userContent'>Hello User 2</div>");
    }
    else {
        echo("<div id='userContent'>Public, non user specific information</div>");
    }

    echo("</body></html>");
?>

But at this point I don't know how to make this site to a dynamic one which shows different information when Python sends a request..

Edit1 - END


Foo Bear
  • 3
  • 2

1 Answers1

0

This is not possible in webdriver currently. The 0 flag refers to the window, not the tab. See a discussion of this on reddit here. Another question also asked this on Stack Overflow but got no answers.

It is possible however with the much more feature rich package Selenium.

import time

from selenium import webdriver

link1="http://mysite/index.php?userID=ABC"
link2="http://mysite/index.php?userID=XYZ"
driver=webdriver.Firefox()
driver.get(link1)
time.sleep(5)
driver.get(link2)

Selenium does require some installation steps: http://selenium-python.readthedocs.io/installation.html

Regarding using requests, there's no reason you can't do a GET request so you don't have to worry about handling a post request:

import requests

url = "http://mysite/index.php?userID=ABC"
res = requests.get(url)
print(res.text)

Without seeing more of your php code or knowing more details, it is hard to say what approach is best for you but this should get you past the issues you mentioned.

Zev
  • 3,423
  • 1
  • 20
  • 41
  • Thanks for your help. I think Selenium would be a little overkill. I just added a little bit of a php sample code with using the GET variables but I don't know how to refresh the divs so that they are showing what python requests.. An other approach would be to insert the current userid into a database and let php check every x seconds if something has changed. But this would not be the best method I think... – Foo Bear Jun 05 '18 at 07:22
  • There is probably a cleaner way but python can post to PHP form using requests. PHP can write the result to a file. JavaScript, Ajax can check the file and update the website. If python can write the file directly then you can skip PHP. Maybe someone else has something cleaner or js could take the lead. The answer to the edit would need a conversation rather than what stack overflow offers. Sounds like a great project! – Zev Jun 05 '18 at 07:41
  • Thanks again. With the help of this site [link](https://stackoverflow.com/questions/6450921/show-a-txt-file-on-a-webpage-which-updates-every-second) I got the website running so a div can update everytime something changed in the txt file. Really not a final solution but it helps to get things running for demonstrating purposes. :) – Foo Bear Jun 06 '18 at 07:46