On Wednesday, February 23, 2011 03:28:31 am Robin Becker wrote:
> I want to automate a remote django view which requires a login with a
> python script, but I am having difficulty in deciding how to make django
> accept the various cookies. Is there example code somewhere which shows
> how this should be done?
>
> I've tried various cookie jar based approaches, but they don't seem to
> work. The test client works through the server code rather than through
> the web.
Have your tried urllib2.Request[1] to POST your login data, how a browser
would do it?
something like this:
Assuming login form:
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField()
# your client script
post_data = {
'username': 'me',
'password': 'pass',
}
data = urllib.urlencode(post_data)
# sends data as a POST[2]
req = urllib2.Request(url='http://mydomain.tld/login', data=data)
resp = urllib1.urlopen(req)
[2] from the link below about the data parameter: Currently HTTP requests are
the only ones that use data; the HTTP request will be a POST instead of a GET
when the data parameter is provided.
Please note this is untested code. You might also want to see the urllib2
missing manual[3]
Mike
[1] http://docs.python.org/library/urllib2.html#urllib2.Request
[3] http://www.voidspace.org.uk/python/articles/urllib2.shtml
--
The course of true anything never does run smooth.
-- Samuel Butler
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.