3 7107
Try this code will give you all cookies will be registered in a file
from the schoolfinder.com -
#!/usr/local/bin/python
-
-
-
-
-
-
COOKIEFILE = 'cookies.lwp' # the path and filename that you want to use to save your cookies in
-
-
import os.path
-
-
import sys
-
-
-
-
cj = None
-
-
ClientCookie = None
-
-
cookielib = None
-
-
-
-
try: # Let's see if cookielib is available
-
-
import cookielib
-
-
except ImportError:
-
-
pass
-
-
else:
-
-
import urllib2
-
-
urlopen = urllib2.urlopen
-
-
cj = cookielib.LWPCookieJar() # This is a subclass of FileCookieJar that has useful load and save methods
-
-
Request = urllib2.Request
-
-
-
-
if not cookielib: # If importing cookielib fails let's try ClientCookie
-
-
try:
-
-
import ClientCookie
-
-
except ImportError:
-
-
import urllib2
-
-
urlopen = urllib2.urlopen
-
-
Request = urllib2.Request
-
-
else:
-
-
urlopen = ClientCookie.urlopen
-
-
cj = ClientCookie.LWPCookieJar()
-
-
Request = ClientCookie.Request
-
-
-
-
####################################################
-
-
# We've now imported the relevant library - whichever library is being used urlopen is bound to the right function for retrieving URLs
-
-
# Request is bound to the right function for creating Request objects
-
-
# Let's load the cookies, if they exist.
-
-
-
-
if cj != None: # now we have to install our CookieJar so that it is used as the default CookieProcessor in the default opener handler
-
-
if os.path.isfile(COOKIEFILE):
-
-
cj.load(COOKIEFILE)
-
-
if cookielib:
-
-
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
-
-
urllib2.install_opener(opener)
-
-
else:
-
-
opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cj))
-
-
ClientCookie.install_opener(opener)
-
-
-
-
# If one of the cookie libraries is available, any call to urlopen will handle cookies using the CookieJar instance we've created
-
-
# (Note that if we are using ClientCookie we haven't explicitly imported urllib2)
-
-
# as an example :
-
-
-
-
theurl = 'http://schoolfinder.com/login/login.asp' # an example url that sets a cookie, try different urls here and see the cookie collection you can make !
-
body={'usr':'greenman','pwd':'greenman'}
-
-
from urllib import urlencode
-
-
-
txdata = urlencode(body) # if we were making a POST type request, we could encode a dictionary of values here - using urllib.urlencode
-
-
txheaders = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'} # fake a user agent, some websites (like google) don't like automated exploration
-
-
-
-
try:
-
-
req = Request(theurl, txdata, txheaders) # create a request object
-
-
handle = urlopen(req) # and open it to return a handle on the url
-
-
except IOError, e:
-
-
print 'We failed to open "%s".' % theurl
-
-
if hasattr(e, 'code'):
-
-
print 'We failed with error code - %s.' % e.code
-
-
elif hasattr(e, 'reason'):
-
-
print "The error object has the following 'reason' attribute :", e.reason
-
-
print "This usually means the server doesn't exist, is down, or we don't have an internet connection."
-
-
sys.exit()
-
-
-
-
else:
-
-
print 'Here are the headers of the page :'
-
-
print handle.info() # handle.read() returns the page, handle.geturl() returns the true url of the page fetched (in case urlopen has followed any redirects, which it sometimes does)
-
-
-
-
print
-
-
if cj == None:
-
-
print "We don't have a cookie library available - sorry."
-
-
print "I can't show you any cookies."
-
-
else:
-
-
print 'These are the cookies we have received so far :'
-
-
for index, cookie in enumerate(cj):
-
-
print index, ' : ', cookie
-
-
cj.save(COOKIEFILE) # save the cookies again
-
-
Thanks for the help. Your code by itself did not work, but it pushed me in the right direction. Here is what worked for me and let me see the protected pages: - #!/usr/bin/env python
-
# -*- coding: UTF-8 -*-
-
-
import cookielib
-
import urllib
-
import urllib2
-
-
cj = cookielib.CookieJar()
-
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
-
resp = opener.open('http://schoolfinder.com') # save a cookie
-
-
theurl = 'http://schoolfinder.com/login/login.asp' # an example url that sets a cookie, try different urls here and see the cookie collection you can make !
-
body={'usr':'greenman','pwd':'greenman'}
-
txdata = urllib.urlencode(body) # if we were making a POST type request, we could encode a dictionary of values here - using urllib.urlencode
-
txheaders = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'} # fake a user agent, some websites (like google) don't like automated exploration
-
-
-
try:
-
req = urllib2.Request(theurl, txdata, txheaders) # create a request object
-
handle = opener.open(req) # and open it to return a handle on the url
-
HTMLSource = handle.read()
-
f = file('test.html', 'w')
-
f.write(HTMLSource)
-
f.close()
-
-
except IOError, e:
-
print 'We failed to open "%s".' % theurl
-
if hasattr(e, 'code'):
-
print 'We failed with error code - %s.' % e.code
-
elif hasattr(e, 'reason'):
-
print "The error object has the following 'reason' attribute :", e.reason
-
print "This usually means the server doesn't exist, is down, or we don't have an internet connection."
-
sys.exit()
-
-
else:
-
print 'Here are the headers of the page :'
-
print handle.info() # handle.read() returns the page, handle.geturl() returns the true url of the page fetched (in case urlopen has followed any redirects, which it sometimes does)
Your script works for me, but the one below for another site does not. The test.html file is not my logged in file like it is when I run your script.
The only lines of code I changed are;
resp = opener.open('http://www.amm.com/')
theurl = 'http://www.amm.com/login.asp'
body={'username':'AMMT54590570','password':'AMMT32 564288'}
What am I doing wrong?
----------------------------------- - #!/usr/bin/env python
-
# -*- coding: UTF-8 -*-
-
-
import cookielib
-
import urllib
-
import urllib2
-
-
cj = cookielib.CookieJar()
-
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
-
resp = opener.open('http://www.amm.com/login.asp') # save a cookie
-
-
theurl = 'http://www.amm.com/login.asp'
-
# an example url that sets a cookie, try different urls here and see the cookie collection you can make !
-
body={'username':'AMMT54590570','password':'AMMT32564288'}
-
txdata = urllib.urlencode(body)
-
# if we were making a POST type request, we could encode a dictionary of values here - using urllib.urlencode
-
txheaders = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
-
# fake a user agent, some websites (like google) don't like automated exploration
-
-
-
try:
-
req = urllib2.Request(theurl, txdata, txheaders) # create a request object
-
handle = opener.open(req) # and open it to return a handle on the url
-
HTMLSource = handle.read()
-
f = file('test.html', 'w')
-
f.write(HTMLSource)
-
f.close()
-
-
except IOError, e:
-
print 'We failed to open "%s".' % theurl
-
if hasattr(e, 'code'):
-
print 'We failed with error code - %s.' % e.code
-
elif hasattr(e, 'reason'):
-
print "The error object has the following 'reason' attribute :", e.reason
-
print "This usually means the server doesn't exist, is down, or we don't have an internet connection."
-
sys.exit()
-
-
else:
-
print 'Here are the headers of the page :'
-
print handle.info() # handle.read() returns the page, handle.geturl() returns the true url of the page fetched (in case urlopen has followed any redirects, which it sometimes does)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Brian Conway |
last post by:
I have no idea what is going on. I have a Login screen where someone types
in their login information and this populates a datagrid based off of the
login. Works great in debug and test through...
|
by: Kris van der Mast |
last post by:
Hi,
I've created a little site for my sports club. In the root folder there are
pages that are viewable by every anonymous user but at a certain subfolder
my administration pages should be...
|
by: Joey Powell |
last post by:
This message was originally posted to the aspnet.security newsgroup,
but no one there has ever heard of this before. That is why I am
posting this message here, so that more people will see it...
...
|
by: Calvin KD |
last post by:
Hi everyone,
Can someone tell me what's wrong with the way that i read a cookie as below:
private void Page_Load(object sender, System.EventArgs e)
{
Response.Cookies.Clear();
HttpCookie...
|
by: pv_kannan |
last post by:
I recently found out that my authentication cookies are not expiring
even though I have set the persist property to false. As a result,
users are able to access the secure websites with indifferent...
|
by: Nicola Farina |
last post by:
Hi all,
I'm testing ASP.NET 1.1 authentications and cookies features, and I've
red tons of tutorials and articles about this, but not all is clear for me.
My goal is to create a basic site...
|
by: studio60podcast |
last post by:
I'm writing an ASP.NET 2.0 application using the new Membership
providor and I'm having trouble.
I have created the roles, logins, login controls, etc... and I can log

in to the site....
|
by: Calvin KD |
last post by:
Hi everyone,
I need help urgently.
I have a C#.Net app which uses cookies for state management. Everything has
been going fine until recently we've expanded the app
and a few more screens were...
|
by: archana |
last post by:
Hi all
I am new to asp.net. I want to implement authentication in all pages.
What i want to do is validate user from database table. So currently
what i am doing is on login page validating...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
|
by: F22F35 |
last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...
| |