python - Handling percentage sign in url -
i've got app processes requests. read variable holds pattern querying using %
wildcard. if url contains patt=m%
i'm fetching names starting m
. worked fine till tried use %d0
search pattern. following error:
the given query string not processed. query strings resource must encoded 'utf8'.
as i've found out using https://www.w3schools.com/tags/ref_urlencode.asp, %d0
string being treated code non-unicode character.
now question is: how handle such patterns %d0
? in other words: how treat 3 characters without encoding?
one workaround i've found far use %25
instead of %
, using patt=%25d
- i'd need handle requests.
edit: here's example. let's take basic cherrpy example tutorial. i've modified handle params
, return value of myvar
:
import cherrypy class helloworld(object): @cherrypy.expose def index(self, **params): #return "hello world!" return cherrypy.request.params['myvar'] if __name__ == '__main__': cherrypy.quickstart(helloworld())
once it's running may open url - default should start on http://localhost:8080/?myvar=blahblahblah
it opens page , returns myvar value: blahblahblah
now, try http://localhost:8080/?myvar=%d0
this cause error i'd prevent.
of course http://localhost:8080/?myvar=%25d0 works fine , shows %d0
.
you can use quote method handle characters in url follows:
import urllib print urllib.quote("patt=m%")
Comments
Post a Comment