Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need a little help with python3 code. I wrote a code for python2, but after a little modifications in python3, I can't get this code working again.

Can you provide me any ideas, what and how can be done, to get this code working again?

#!/usr/bin/env python
#
# python3 version http client
# 

import urllib.parse
import urllib.request
import time

param1 = 'value1'
param2 = 'value2'

tm = 60

while True:
    url = "http://a.w.com/?link=11&u=10"
    values = {'param1':value1, 'param2':value2}

    data = urllib.parse.urlencode(values)
    binary_data = data.encode('ascii')
    req = urllib.request.Request(url, binary_data)
    urllib.request.urlopen(req)

    # find only cmd output:
    result = re.search('Wireless(.*)</div>', data)
    print (result.group(1))

Code seems to be ok, but I need to add here basic auth login functionality. I know that in Python 3 auth was changed to oauth, but I don't know how should I prepare this code to working.

Any idea is welcome.

share|improve this question
2  
This question somehow lacks a relation to Ubuntu. –  Takkat May 24 '13 at 8:11
1  
If the OP is using Ubuntu and chooses to post his question here it is totally on topic for the site. No further explanations needed. –  Bruno Pereira May 24 '13 at 8:32
1  
This isn't the code you are using, it contains multiple errors (just from sight alone). Provide a minimal working example and errormessages. –  Timo May 24 '13 at 9:17

1 Answer 1

I'm not sure what you are trying to do but if any idea is welcome I'd suggest using requests instead of urllib.

http://docs.python-requests.org/en/latest/index.html

If you need authentication ideas, they are here for you.

http://docs.python-requests.org/en/latest/user/authentication.html

share|improve this answer
    
+1 for python-requests over urllib. Way more intuitive to accomplish this. –  gertvdijk May 24 '13 at 9:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.