force URLencoding script

r0g aioe.org at technicalbloke.com
Fri Jan 15 09:38:34 EST 2010


João wrote:
> On Jan 14, 5:58 pm, r0g <aioe.... at technicalbloke.com> wrote:
>> João wrote:
>>> On Jan 12, 10:07 pm, r0g <aioe.... at technicalbloke.com> wrote:
>>>> João wrote:
>>> for the following data,
>>> authentication = "UID=somestring&"
>>> message = 'PROBLEM severity High: OperatorX Plat1(locationY) global
>>> Succ. : 94.470000%'
>>> dest_number = 'XXXXXXXXXXX'
>>> url_values = urlencode({'M':message})
>>> enc_data = authentication + url_values + dest_number
>>> I'm getting null for
>>> full_url = Request(url, enc_data, headers)
>>> and thus,
>>> response = urlopen(full_url).read()
>>> returns,
>>> TypeError: <exceptions.TypeError instance at 0x2b4d88ec6440>
>>> )
>> Are you sure it's returning a null and not just some other unexpected
>> type?
>>
>> I think your problem may be that you are passing a urllib2 class to
>> urllib(1)'s urlopen. Try using urllib2's urlopen instead e.g.
>>
>> import urllib2
>> request_object = urllib2.Request('http://www.example.com')
>> response = urllib2.urlopen(request_object)
>> the_page = response.read()
>>
>> Roger.
> 
> Thanks Roger.
> I think it's a null because i did a print(full_url) right after the
> Request
> I tried
> request_object = urllib2.Request('http://www.example.com')
> print(request_object)
> 
> but when printing I get: <urllib2.Request instance at 0x2afaa2fe3f80>

Hi João,

That's exactly what you want, an object that is an instance of the
Request class. That object doesn't do anything by itself, you still need
to a) Connect to the server and request that URL and b) Read the data
from the server.

a) To connect to the web server and initialize the request you need to
call urllib2.urlopen() with the Request object you just created and
assign the result to a name e.g.

>> response = urllib2.urlopen(request_object)


That will give you an object (response) that you can call the .read()
method of to get the web page data.

>> the_page = response.read()



If that doesn't make sense or seem to work for you then please try
reading the following website from top to bottom before taking any
further steps...

http://www.voidspace.org.uk/python/articles/urllib2.shtml


> 
> I've read about Python 2.4 not playing well with proxies even with no
> proxy activated.
> Any sugestion?

I doubt any language can play well with proxies if there are none so I
doubt it's a factor ;)

Good luck,

Roger.



More information about the Python-list mailing list