post - Python automating a wget script with login required -


i need automate download process site requires following:

  • send http post request containing username , password
  • i should cookie (probably containing session id)
  • send http request file, sending cookie details in http headers

using wget now, must first login password (open session?):

wget --no-check-certificate -o /dev/null --save-cookies auth.rda_ucar_edu --post-data=email=name@domain.edu&passwd=5555&action=login https://rda.ucar.edu/cgi-bin/login  

then, retrieve files need:

wget --no-check-certificate -n --load-cookies auth.rda_ucar_edu http://rda.ucar.edu/data/ds608.0/3hrly/1979/narrflx_197901_0916.tar 

is there nice way in python? have tried many ways , have not gotten work. following python code seems log me in correctly. however, believe need keep session live while download data?

url = 'https://rda.ucar.edu/cgi-bin/login' values = {'email': 'name@domain.edu', 'password': '5555', 'action': 'login'} data = urllib.urlencode(values) binary_data = data.encode('ascii') req = urllib2.request(url, binary_data) response = urllib2.urlopen(req) print response.read() 

have tried this:

from requests import session session() c:     c.post(url, values)     request = c.get('http://rda.ucar.edu/data/ds608.0/3hrly/1979/narrflx_197901_0108.tar') 

any suggestions helpful.

you need save cookies.

easier use 3rd party lib mechanize or scrapy though


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -