Python com Proxy [resolvido]

Iniciado por pedropehrson, 11 de Maio de 2010, 15:16

tópico anterior - próximo tópico

pedropehrson

Ola pessoal tudo bem?

Estou com um problema em um script em python que nao consegue passar pelo  proxy.

Acredito eu que o python tenh que setar um proxy por ele direto, ele nao puxa o proxy do sistema.

bom o script é esse aqui:


import urllib2
import sys

if sys.argv > 1: #arg must be supplied
       stocks = sys.argv #args are the stocks
       stocks.pop(0) #pop the first entry since its the filename
       print
       print '%5s %8s %9s %9s %9s' % ('Name','Value','Open','Change','Time') #print header
       print '--------------------------------------------' #fancy line

       for i in stocks: #for each stock do this
               stock=i

               #This is the url from yahoo...if this changes the script dies, Ill fix it and upload again if it happens.
               csv = urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s='+ stock +'&f=sl1d1t1c1ohgv&e=.csv')
               #csv = urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s=CAKE&f=sl1d1t1c1ohgv&e=.csv')
               data = csv.read() #grab csv
               tokens = data.split(',') #split into tokens

               name=tokens[0].replace('"','') #get rid of quotes
               value=tokens[1]
               time=tokens[3].replace('"','')
               change=tokens[4]
               openVal=tokens[5]

               print '%5s' % name,'%8s'% value,'%9s'% openVal,'%9s'% change,'%9s'% time #print to screen with formatting
else:
       print 'You forgot to supply your stock quote as an arg' #go back and give the script your stocks as arguments
       print 'Example: python stocks.py goog'


Eu consigo chamar ele normalmente na rede sem proxy, porem com a rede com proxy acontece essa msg aqui:

Traceback (most recent call last):
 File "/etc/conky/stocks.py", line 31, in <module>
   csv = urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s='+ stock +'&f=sl1d1t1c1ohgv&e=.csv')
 File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
   return _opener.open(url, data, timeout)
 File "/usr/lib/python2.6/urllib2.py", line 397, in open
   response = meth(req, response)
 File "/usr/lib/python2.6/urllib2.py", line 510, in http_response
   'http', request, response, code, msg, hdrs)
 File "/usr/lib/python2.6/urllib2.py", line 435, in error
   return self._call_chain(*args)
 File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
   result = func(*args)
 File "/usr/lib/python2.6/urllib2.py", line 518, in http_error_default
   raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required


Alguem sabe como é que coloca proxy no python? Cheguei a abrir o urllib2.py mas nao achei um lugar certo para "proxyar"


Fico no aguardo galera, abcs
















Resolucao:


if sys.argv > 1: #arg must be supplied
   stocks = sys.argv #args are the stocks
   stocks.pop(0) #pop the first entry since its the filename
   print
   print '%5s %8s %9s %9s %9s' % ('Name','Value','Open','Change','Time') #print header
   print '--------------------------------------------' #fancy line

   for i in stocks: #for each stock do this
      stock=i
      ############proxy#####################
      passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
      passmgr.add_password(None, 'http://proxy:3128', 'seu_login_aqui', 'sua_senha_aqui')
      authinfo = urllib2.ProxyBasicAuthHandler(passmgr)
      proxy_support = urllib2.ProxyHandler({"http" : "http://proxy:3128"})

      opener = urllib2.build_opener(proxy_support, authinfo)
      urllib2.install_opener(opener)

      ###########fim do proxy###############
      #This is the url from yahoo...if this changes the script dies, Ill fix it and upload again if it happens.
      csv = urllib2.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s='+ stock +'&f=sl1d1t1c1ohgv&e=.csv')
      data = csv.read() #grab csv
      tokens = data.split(',') #split into tokens

      name=tokens[0].replace('"','') #get rid of quotes
      value=tokens[1]
      time=tokens[3].replace('"','')
      change=tokens[4]
      openVal=tokens[5]

      print '%5s' % name,'%8s'% value,'%9s'% openVal,'%9s'% change,'%9s'% time #print to screen with formatting
else:
   print 'You forgot to supply your stock quote as an arg' #go back and give the script your stocks as arguments
   print 'Example: python stocks.py goog'



Abracos! isso dai da pra usar no conky e ele fica mostrando as cotacoes!

abcs