force URLencoding script

João joaopcf at gmail.com
Tue Jan 12 21:11:06 CET 2010


On Jan 12, 8:05 pm, r0g <aioe.... at technicalbloke.com> wrote:
> João wrote:
> > Someone please?
>
> Haven't seen your original post yet mate, usenet can be flaky like that,
> might have been a good idea to quote your original post!
>
> Roger.

Thanks Roger.

> João wrote:
> > Someone please?
> > Hi.
> > I'm trying to figure out how to force URLencoding in my Python 2.4.3
> > environment, receiving data as an input argument but I'm really at a loss
> > here.

> > What am I doing wrong?

#!/usr/bin/env python

import sys
from urllib import urlencode, urlopen
from urllib2 import Request
import urlparse

destination = sys.argv[1]
msg = sys.argv[2] #Will I have problems with this one if the input is
multiline?

# the browser identifies itself using the User-Agent header
# after creating the Request object, it's possible to pass in a
dictionary of headers
user_agent  =  'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
SV1; .NET CLR 1.1.4322)' # force the request to be identified as IE
5.5....
headers =  {  ’User-Agent’  :  user_agent  }

# force no proxy

authentication = 'UID=22541&PW=gdyb21LQTcIANtvYMT7QVQ==&'
# force Unicode display format
message = u'M=%s&' % msg
dest_number = 'N=%s' % destination
data = authentication + message + dest_number

url = 'http://10.112.28.221:38080/GwHTTPin/sendtext'
print 'Encoded URL:', url

#get full URL adding ? to it, followed by the encoded values
#full_url = url + '?' url_values
#should I force url_values = urllib.urlencode(data) instead?
full_url = urllib2.Request(url, data, headers)

response = urllib2.urlopen(full_url) #.urlopen works transparently
with proxies which do not require authentication

processed = urllib.open(full_url)



More information about the Python-list mailing list