Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url encode using python 2.7

>>> import httplib
>>> x = httplib.HTTPConnection('localhost', 8080)
>>> x.connect()
>>> x.request('GET','/camera/store?fn=aaa&ts='+str.encode('2015-06-15T14:45:21.982600+00:00','ascii')+'&cam=ddd')
>>> y=x.getresponse()
>>> z=y.read()
>>> z

'error: Invalid format: "2015-06-15T14:45:21.982600 00:00" is malformed at " 00:00"'

And the system show me this error. As i want to encode this format to this: 2015-06-15T14%3A45%3A21.982600%2B00%3A00

like image 308
Kevin Kai Avatar asked May 31 '16 09:05

Kevin Kai


People also ask

What is %20 URL encode?

URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

What does %2f mean in a URL?

URL encoding converts characters into a format that can be transmitted over the Internet. - w3Schools. So, "/" is actually a seperator, but "%2f" becomes an ordinary character that simply represents "/" character in element of your url.

How do I remove 20 from a URL in Python?

replace('%20+', '') will replace '%20+' with empty string.


2 Answers

>>> import urllib
>>> f = { 'fn' : 'aaa', 'ts' : "2015-06-15T14:45:21.982600+00:00"}
>>> urllib.urlencode(f)

from:

How to urlencode a querystring in Python?

like image 108
Destrif Avatar answered Oct 05 '22 00:10

Destrif


url = "http://example.com?p=" + urllib.quote(query)

it works with this!

like image 43
Kevin Kai Avatar answered Oct 05 '22 01:10

Kevin Kai