I am trying to connect to a private page were you have to be logged in to view it using urllib. When I try to connect to the page I just get redirected, to the login page.
Is there a way to log in with urllib or use cookies from my webrowser or something like that?
I have tried to figure out how to do it myself and have failed.
Any help on this would be nice.
If your page uses HTML authentication, use HTTPBasicAuthHandler.
If your page uses authentication by form, use POST request to send login form and store the cookies using cookielib.
Look for Authentication under http://docs.python.org/library/urllib2.html#examples
Related
I have a link: https://uchebnik.mos.ru/exam/test/test_by_binding/2511452/homework/152004494/variant/45906101/num/1?generation_context_type=lesson&external_binding_id=2230159&referer=homework®istration={my_token}
If I have logged in to the site, then I should be automatically redirected by the link: https://uchebnik.mos.ru/exam/test/training_spec/124085/task/1?registration={my_token}
I use cookies for authorization: "auth_token", "profile_id", "udacl".
How i can get this redirect link?
I use urllib.open, request.url, but the link does not change.
My guess is that this is due to JavaScript authorization, but I tried the requests_html library with JS support, but the result does not change.
Preferably without using webdrivers.
I started recently working on API's and few API providers are not requesting redirect URL while some others are requesting. I have written an algorithmic strategy for trading using python. When I requested for API to Fyers(stockbroker), the team said me to provide a redirect URL. what is a redirect URL? and how to create it?
I have attached image for reference. In the above image, there is a text box for Redirect URL. Can you please explain what exactly is Redirect URL and how to create one for calling API for authentication if my code is on heroku?
The Redirect URL is required by the oAuth workflow: basically the authorisation server will redirect the user back to the URL registered as "Redirect URL" including an authorization code or a token.
If you register a URL like https://myapp.herokuapp.com you will be redirected to
https://myapp.herokuapp.com?access_code=XXX&app_id=YYY
The Redirect URL needs to be a valid accessible page: if the process is manual you just copy the access_code from the browser and use it accordingly.
If it is an application you need to receive the redirect above (the URL is basically your app), fetch the required information (parameters) and implement your logic.
Default Fyers Redirect URL for Testing
Use the default url from fyers
https://trade.fyers.in/api-login/redirect-uri/index.html
Copy the auth key value
Use it in your python app in the second run
You can also use google collab, to run part of code only (authentication) without restarting the whole project
I can get html of a web site using lxml module if authentication is not required. However, when it required, how do I input 'User Name' and 'Password' using python?
It very much depends on the method of authentication used. If it's HTTP Basic Auth, then you should be able to pass those headers along with the request. If it's using a web page-based login, you'll need to automate that request and pass back the cookies or whatever session token is used with the next request.
I'm not sure if such a thing is possible, but I am trying to submit to a form such as https://lambdaschool.com/contact using a POST request.
I currently have the following:
import requests
payload = {"name":"MyName","lastname":"MyLast","email":"someemail#gmail.com","message":"My message"}
r = requests.post('http://lambdaschool.com/contact',params=payload)
print(r.text)
But I get the following error:
<title>405 Method Not Allowed</title>
etc.
Is such a thing possible to submit using a POST request?
If it were that simple, you'd see a lot of bots attacking every login form ever.
That URL obviously doesn't accept POST requests. That doesn't mean the submit button is POST-ing to that page (though clicking the button also gives that same error...)
You need to open the chrome / Firefox dev tools and watch the request to see what happens on form submit and replicate that data in Python.
Another option would be the mechanize or Selenium webdriver libraries to simulate a browser and fill out the form
params is for query parameters. You either want data, for a form encoded body, or json, for a JSON body.
I think the url should be 'http://lambdaschool.com/contact-form'.
I am trying to automate files download via a webserver. I plan on using wget or curl or python urllib / urllib2.
Most solutions use wget and urllib and urllib2. They all talk of HHTP based authentication and cookie based authentication. My problem is I dont know which one is used in the website that stores my data.
Here is the interaction with the site:
Normally I login to site http://www.anysite.com/index.cgi?
I get a form with a login and password. I type in both and hit return.
The url stays as http://www.anysite.com/index.cgi? during the entire interaction. But now I have a list of folders and files
If I click on a folder or file the URL changes to http://shamrockstructures.com/cgi-bin/index.cgi?page=download&file=%2Fhome%2Fjanysite%2Fpublic_html%2Fuser_data%2Fuserareas%2Ffile.tar.bz2
And the browser offers me a chance to save the file
I want to know how to figure out whether the site is using HTTP or cookie based authentication. After which I am assuming I can use cookielib or urllib2 in python to connect to it, get the list of files and folders and recursively download everything while staying connected.
p.S: I have tried the cookie cutter ways to connect via wget and wget --http-user "uname" --http-password "passwd" http://www.anysite.com/index.cgi? , but they only return the web form back to me.
If you log in using a Web page, the site is probably using cookie-based authentication. (It could technically use HTTP basic auth, by embedding your credentials in the URI, but this would be a dumb thing to do in most cases.) If you get a separate, smallish dialog with a user name and password field (like this one), it is using HTTP basic authentication.
If you try to log in using HTTP basic auth, and get back the login page, as is happening to you, this is a certain indication that the site is not using HTTP basic auth.
Most sites use cookie-based authentication these days. To do this with an HTTP cilent such as urllib2, you will need to do an HTTP POST of the fields in the login form. (You may need to actually request the login form first, as a site could include a cookie that you need to even log in, but usually this is not necessary.) This should return a "successfully logged in" page that you can test for. Save the cookies you get back from this request. When making the next request, include these cookies. Each request you make may respond with cookies, and you need to save those and send them again with the next request.
urllib2 has a function called a "cookie jar" which will automatically handle the cookies for you as you send requests and receive Web pages. That's what you want.
You can use pycurl like this:
import pycurl
COOKIE_JAR = 'cookiejar' # file to store the cookies
LOGIN_URL = 'http://www.yoursite.com/login.cgi'
USER_FIELD = 'user' # Name of the element in the HTML form
USER = 'joe'
PASSWD_FIELD = 'passwd' # Name of the element in the HTML form
PASSWD = 'MySecretPassword'
def read(html):
"""Read the body of the response, with posible
future html parsing and re-requesting"""
print html
com = pycurl.Curl()
com.setopt(pycurl.WRITEFUNCTION, read)
com.setopt(pycurl.COOKIEJAR, COOKIE_JAR)
com.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirects
com.setopt(pycurl.POST, 1)
com.setopt(pycurl.POSTFIELDS, '%s=%s;%s=%s'%(USER_FIELD, USER,
PASSWD_FIELD, PASSWD))
com.setopt(pycurl.URL, LOGIN_URL )
com.perform()
Plain pycurl it may seam very "primitive" (with the limited setopt approach),
but it gets the job done, and handle pretty well the cookies with the cookie jar option.
AFAIK cookie based authentication is only used once you have logged in successfully atleast ONCE. You can try disabling storing cookies from that domain by changing your browser settings, if you are still able to download files that it should be a HTTP based authentication.
Try doing a equivalent GET request for the (possibly POST) login request that is probably happening right now for login. Use firebug or fiddler to see the login request that is sent.
Also note if there is some javascript code which is returning you a different output, based on your useragent string or some other parameter.
See if httplib, mechanize helps.