How to set "User-Agent" in script?
#1
I have a script I am working on that uses urllib2. I am trying to change the default "User-Agent" that is used because the backend is looking for a certain User-Agent. I am having no success. Below is the code I am using but it is not working to change the User-Agent. Any help is appreciated.

Code:
    request = urllib2.Request(url, headers={"User-Agent" : "CUSTOM_USER_AGENT", "Accept" : "application/xml"})
u = urllib2.urlopen(request)

I can not figure out what I am doing wrong here...
Reply
#2
I have a script I am working on that uses urllib2. I am trying to change the default "User-Agent" that is used because the backend is looking for a certain User-Agent. I am having no success. Below is the code I am using but it is not working to change the User-Agent. Any help is appreciated.

Code:
request = urllib2.Request(url, headers={"User-Agent" : "CUSTOM_USER_AGENT", "Accept" : "application/xml"})
u = urllib2.urlopen(request)

I can not figure out what I am doing wrong here...
Reply
#3
Is this even kodi related?
AppleTV4/iPhone/iPod/iPad: HowTo find debug logs and everything else which the devs like so much: click here
HowTo setup NFS for Kodi: NFS (wiki)
HowTo configure avahi (zeroconf): Avahi_Zeroconf (wiki)
READ THE IOS FAQ!: iOS FAQ (wiki)
Reply
#4
looks correct to me.
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#5
The script is a kodi script so I thought it would be... if I am wrong for asking I apologize but it is for a Kodi script...
Reply
#6
Yeah, I thought so too. But for whatever reason it does not send a User-Agent. It always only sends the Kodi UA ("Kodi/16.1 (Macintosh; Intel Mac OS X 10_11_6) App_Bitness/64 Version/16.1-Git:2016-04-24-c327c5" in my case.)
Reply
#7
If you mean setting the user-agent for Player/playback. You would format the url with curl headers appended something like:

untested from memory
Code:
url += '|User-Agent=%s' % urllib2.quote('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36', safe='')

and use that url with Player/setResolvedUrl
Debug Log (wiki) | Troubleshooting (wiki)Add-ons
Reply
#8
(2016-12-05, 14:48)anxdpanic Wrote: If you mean setting the user-agent for Player/playback. You would format the url with curl headers appended something like:

untested from memory
Code:
url += '|User-Agent=%s' % urllib2.quote('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36', safe='')

and use that url with Player/setResolvedUrl

No joy, unless I am doing it wrong still. The URL requires parameters to be passed to it also (?username=xxxx&password=xxxx) and when trying this method I get login failures....
Reply
#9
Should still work fine if the url has parameters. it may be that it is urllib.quote_plus() and not quote.

Here is an older thread which discussed it. http://forum.kodi.tv/showthread.php?tid=77500
Debug Log (wiki) | Troubleshooting (wiki)Add-ons
Reply
#10
(2016-12-05, 20:27)dustintinsley Wrote:
(2016-12-05, 14:48)anxdpanic Wrote: If you mean setting the user-agent for Player/playback. You would format the url with curl headers appended something like:

untested from memory
Code:
url += '|User-Agent=%s' % urllib2.quote('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36', safe='')

and use that url with Player/setResolvedUrl

No joy, unless I am doing it wrong still. The URL requires parameters to be passed to it also (?username=xxxx&password=xxxx) and when trying this method I get login failures....

Maybe have a look at this thread.
Kodi Nexus on Dell Optiplex 980 Lubuntu 20.04 | Kodi Nexus on HTPC Lubuntu 20.04 | My Add-ons | Legacy Repo | Matrix Repo
Reply
#11
(2016-12-06, 01:10)MetalChris Wrote:
(2016-12-05, 20:27)dustintinsley Wrote:
(2016-12-05, 14:48)anxdpanic Wrote: If you mean setting the user-agent for Player/playback. You would format the url with curl headers appended something like:

untested from memory
Code:
url += '|User-Agent=%s' % urllib2.quote('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36', safe='')

and use that url with Player/setResolvedUrl

No joy, unless I am doing it wrong still. The URL requires parameters to be passed to it also (?username=xxxx&password=xxxx) and when trying this method I get login failures....

Maybe have a look at this thread.

Thanks to everyone, I got it working. Huge help.... Thanks!
Reply

Logout Mark Read Team Forum Stats Members Help
How to set "User-Agent" in script?0