Python extreme novice, seeks guidance on a "requests" issue

Posted on
Tue May 10, 2016 7:00 am
mat offline
Posts: 769
Joined: Nov 25, 2010
Location: Cambridgeshire - UK

Python extreme novice, seeks guidance on a "requests" issue

Good afternoon.

Im seeking a little guidance, or help on the following. Apparently its a fairly simple issue, but Im struggling.

I'm trying to control my Viera Panasonic TV, and if I can get the basic code working, will look to construct what I hope is a simple plugin. I just cant get the python to work.

I have the following xml data that is posted with headers to the TV.

XML

<?xml version"1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlso.../soap/envelope/" s:encodingStyle="http://schemas.xmlso.../soap/encoding/">
<s:Body>
<u:X_SendKey xmlns:u="urn:panasonic-com:service:p00NetworkControl:1">
<X_KeyEvent>NRC_MUTE-ONOFF</X_KeyEvent>
</u:X_SendKey>
</s:Body>
</s:Envelope>


Header

Accept: text/xml
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/xml;charset="utf-8"
SOAPACTION: "urn:panasonic-com:service:p00NetworkControl:1#X_SendKey\"
Content-Length:324


Posted 'to' (http://192.168.1.55:55000/nrc/control_0)

Now, I can get this to work from Cromes Postman app all well and good, and can send any of the commands needed to control the TV.

The Postman app generates the python code below, which requires the 'requests' module, which I have confirmed is installed for python 2.6. However, when I run the code below on the same machine as Postman (or any other computer), I get a 400 error from the TV. I've tried a script within indigo and from PyCharm.

I've been looking at this for a week or so, and would be grateful for some guidance please. Thanks.

Code: Select all
import requests

url = "http://192.168.1.55:55000/nrc/control_0"

payload = "<?xml version\"1.0\" encoding=\"utf-8\"?>\n<s:Envelope xmlns:s=\"http://schemas.xmlso.../soap/envelope/\" s:encodingStyle=\"http://schemas.xmlso.../soap/encoding/\">\n<s:Body>\n<u:X_SendKey xmlns:u=\"urn:panasonic-com:service:p00NetworkControl:1\">\n<X_KeyEvent>NRC_MUTE-ONOFF</X_KeyEvent>\n</u:X_SendKey>\n</s:Body>\n</s:Envelope>"
headers = {
    'accept': "text/xml",
    'cache-control': "no-cache",
    'pragma': "no-cache",
    'content-type': "text/xml;charset=\"utf-8\"",
    'soapaction': "\"urnpanasonic-comservicep00NetworkControl1#X_SendKey\\"",
    'content-length': "324",
    'postman-token': "e17a5e11-b628-faa7-cac3-7391c01d8c0f"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

Late 2018 mini 10.14

Posted on
Tue May 10, 2016 11:10 am
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python extreme novice, seeks guidance on a "requests" is

It looks like the length is incorrect. You should calculate the lengh:

Code: Select all
import requests

url = "http://192.168.1.55:55000/nrc/control_0"

payload = u"<?xml version\"1.0\" encoding=\"utf-8\"?>\n<s:Envelope xmlns:s=\"http://schemas.xmlso.../soap/envelope/\" s:encodingStyle=\"http://schemas.xmlso.../soap/encoding/\">\n<s:Body>\n<u:X_SendKey xmlns:u=\"urn:panasonic-com:service:p00NetworkControl:1\">\n<X_KeyEvent>NRC_MUTE-ONOFF</X_KeyEvent>\n</u:X_SendKey>\n</s:Body>\n</s:Envelope>"
headers = {
    'accept': "text/xml",
    'cache-control': "no-cache",
    'pragma': "no-cache",
    'content-type': "text/xml;charset=\"utf-8\"",
    'soapaction': "\"urnpanasonic-comservicep00NetworkControl1#X_SendKey\\"",
    'content-length': len(payload),
    'postman-token': "e17a5e11-b628-faa7-cac3-7391c01d8c0f"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue May 10, 2016 1:10 pm
mat offline
Posts: 769
Joined: Nov 25, 2010
Location: Cambridgeshire - UK

Re: Python extreme novice, seeks guidance on a "requests" is

Thanks Jay, I had recreated code in the office. point taken on calculating len.

Using code below with the length addition from plugin menu items, and its not working.

Event log is showing

Example Device - Custom Debug startup called
Script Starting new HTTP connection (1): 192.168.1.55


Not working from within Pycharm either, which is returning with "process finished with exit code 0", and response =400

Code: Select all
import requests

url = "http://192.168.1.55:55000/nrc/control_0/"

payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n<s:Body>\n<u:X_SendKey xmlns:u=\"urn:panasonic-com:service:p00NetworkControl:1\">\n<X_KeyEvent>NRC_POWER-ONOFF</X_KeyEvent>\n</u:X_SendKey>\n</s:Body>\n</s:Envelope>"
headers = {
    'accept': "text/xml",
    'cache-control': "no-cache",
    'pragma': "no-cache",
    'soapaction': "\"urnpanasonic-comservicep00NetworkControl1#X_SendKey\"",
    'content-length': len(payload),
    'content-type': "application/xml",
    'postman-token': "2784b484-ac90-5eb4-8895-f93d98d4e86e"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

Late 2018 mini 10.14

Posted on
Wed May 11, 2016 4:42 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python extreme novice, seeks guidance on a "requests" is

Sorry - the server just isn't accepting the message for some reason. I don't know why. Try this:

Code: Select all
import requests

url = "http://192.168.1.55:55000/nrc/control_0/"

payload = '''<?xml version"1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlso.../soap/envelope/" s:encodingStyle="http://schemas.xmlso.../soap/encoding/">
<s:Body>
<u:X_SendKey xmlns:u="urn:panasonic-com:service:p00NetworkControl:1">
<X_KeyEvent>NRC_MUTE-ONOFF</X_KeyEvent>
</u:X_SendKey>
</s:Body>
</s:Envelope>'''
headers = {
    'accept': "text/xml",
    'cache-control': "no-cache",
    'pragma': "no-cache",
    'soapaction': "\"urnpanasonic-comservicep00NetworkControl1#X_SendKey\"",
    'content-length': len(payload),
    'content-type': "application/xml",
    'postman-token': "2784b484-ac90-5eb4-8895-f93d98d4e86e"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)


Just in case the string is getting munged. If that doesn't work, then try adding a carriage return before the final ''' in case the message has to end with a line feed. Pretty sad that the destination system doesn't give you any clues other than a 400 error... :roll:

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu May 12, 2016 2:09 pm
mat offline
Posts: 769
Joined: Nov 25, 2010
Location: Cambridgeshire - UK

Re: Python extreme novice, seeks guidance on a "requests" is

Thanks Jay.

Tried both those options earlier and neither worked unfortunately.

I have however found a github project https://github.com/tomokas/pyviera that I am going to attempt to work into a plugin (theres no licence info though, so have emailed the author). I've got the script loaded up on my laptop and it runs perfectly, controlling all 4 tv's independently. It should control all panasonic TV's from around 2012 so hopefully will be of use to some if the guy confirms I can use a good proportion of his code. It may well work with panasonic b/r players and hard drive recorders too. I have one that i can test on so will confirm.

Thanks for you persistence as always. I may well be asking the community for pointers on the plugin soon!

Mat

Late 2018 mini 10.14

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests