A question about the urllib2 ?

B

Bo Yang

Hi ,
Recently I use python's urllib2 write a small script to login our
university gateway .
Usually , I must login into the gateway in order to surf the web . So ,
every time I
start my computer , it is my first thing to do that open a browser to
login the gateway !

So , I decide to write such a script , sending some post information to
the webserver
directly to login automatic once the computer is on . And I write the
code below :

urllib2.urlopen(urllib2.Request(url="https://202.113.16.223/php/user_login.php",
data="loginuser=0312889&password=o127me&domainid=1&refer=1& logintype=
££££££££££"))

In the five '#' above , I must submit some Chinese character , but the
urllib2 complain
for the non-ascii characters .

What do you think this ?

Any help will be appreciated very much , thanks in advance !
 
F

Fuzzyman

Bo said:
Hi ,
Recently I use python's urllib2 write a small script to login our
university gateway .
Usually , I must login into the gateway in order to surf the web . So ,
every time I
start my computer , it is my first thing to do that open a browser to
login the gateway !

So , I decide to write such a script , sending some post information to
the webserver
directly to login automatic once the computer is on . And I write the
code below :

urllib2.urlopen(urllib2.Request(url="https://202.113.16.223/php/user_login.php",
data="loginuser=0312889&password=o127me&domainid=1&refer=1& logintype=
#####"))

In the five '#' above , I must submit some Chinese character , but the
urllib2 complain
for the non-ascii characters .

What do you think this ?
I haven't tried this, so I'm guessing :)

Do you pass in the string to urllib2 as unicode ? If so, try encoding
it to UTF8 first...

Otherwise you might try escaping it using ``urllib.quote_plus``. (Note
``urllib``, *not* ``urllib2``.)

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
B

Bo Yang

Fuzzyman 写é“:
I haven't tried this, so I'm guessing :)

Do you pass in the string to urllib2 as unicode ? If so, try encoding
it to UTF8 first...

Otherwise you might try escaping it using ``urllib.quote_plus``. (Note
``urllib``, *not* ``urllib2``.)

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Thank you , I have got it !
I quote the Chinese with urllib.quote() ,
Thanks again !
 
S

Serge Orlov

Bo said:
Hi ,
Recently I use python's urllib2 write a small script to login our
university gateway .
Usually , I must login into the gateway in order to surf the web . So ,
every time I
start my computer , it is my first thing to do that open a browser to
login the gateway !

So , I decide to write such a script , sending some post information to
the webserver
directly to login automatic once the computer is on . And I write the
code below :

urllib2.urlopen(urllib2.Request(url="https://202.113.16.223/php/user_login.php",
data="loginuser=0312889&password=o127me&domainid=1&refer=1& logintype=
#####"))

In the five '#' above , I must submit some Chinese character , but the
urllib2 complain
for the non-ascii characters .

I guess the server expect that a browser is coming from page
https://202.113.16.223/, so the url should be submitted in the encoding
of page https://202.113.16.223/ which is gb2312.

urllib2.urlopen(urllib2.Request(url="https://202.113.16.223/php/user_login.php",
data=u"loginuser=0312889&password=o127me&domainid=1&refer=1& logintype=
写é“".encode('gb2312')))

should work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top