Python urllib.open issue
#1
Hello,

This is a pretty general Python question, but relates to the streaming app I am working on.

I have a url opener defined as such:

Code:
class BrowseBot:
    def __init__(self):
        cookie_handler= urllib2.HTTPCookieProcessor()
        redirect_handler= urllib2.HTTPRedirectHandler()
        self._opener = urllib2.build_opener(redirect_handler, cookie_handler)

    def GET(self, url):
        return self._opener.open(url).read()

    def POST(self, url, parameters):
        return self._opener.open(url, urllib.urlencode(parameters)).read()

I am trying to do a GET/POST on a url of the form "http://www.example.com/#/foo" however, the URL always truncates after the #. It prints to console fine, showing the full URL, but when intercepting the POST/GET I notice that it is truncated.

Can anyone provide insite to this issue?

(Its very hard to Google this since # = python comment!!! Sad )
Reply
#2
xceph Wrote:Hello,

This is a pretty general Python question, but relates to the streaming app I am working on.

I have a url opener defined as such:

Code:
class BrowseBot:
    def __init__(self):
        cookie_handler= urllib2.HTTPCookieProcessor()
        redirect_handler= urllib2.HTTPRedirectHandler()
        self._opener = urllib2.build_opener(redirect_handler, cookie_handler)

    def GET(self, url):
        return self._opener.open(url).read()

    def POST(self, url, parameters):
        return self._opener.open(url, urllib.urlencode(parameters)).read()

I am trying to do a GET/POST on a url of the form "http://www.example.com/#/foo" however, the URL always truncates after the #. It prints to console fine, showing the full URL, but when intercepting the POST/GET I notice that it is truncated.

Can anyone provide insite to this issue?

(Its very hard to Google this since # = python comment!!! Sad )

surely "http://www.example.com/#/foo" is saying 'go to http://www.example.com/ and scroll to the anchor called '/foo'? so isn't that behaviour correct? (browsers behave this way too)

if what you really want to do is send '#' as part of a url you need to url encode it (it will become '%23'). you can use urllib.quote_plus() to do that.

t0mm0
Reply
#3
Thanks, that probably is the case. Not very familiar with web-techs, this is me getting my feet wet Smile
Reply

Logout Mark Read Team Forum Stats Members Help
Python urllib.open issue0