Online Shopping : Computers : Programming : Languages : Python : Common Problems : Web Programming

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

29 of 31 people (94%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do you POST data to an HTTPS: server? Any examples?

Nov 18th, 2000 12:30
Jose Correia,


I have managed to find an answer myself to this question.  This is a 
solution for the Windows environment.  It involves using COM and MSIE 
and requires no manual handling of either SSL or cookies, etc.  It's 
really painfully easy once you get pointed in the right direction.
My special thanks to Michal Wallace whose newsgroup posting a while 
back answering someone else's question helped get me on the right 
track. (some parts below are cut directly from his posting).
Instructions:
1) Install Win32 extensions.
2) Once you have them installed:
   - open pythonwin
   - load the "COM Makepy utility" under the Tools menu
   - in the window that pops up, double click on:
     "Microsoft Internet Controls"
   This generates a magical python module for you...
3) Now, in the python window, type:
   >>> from win32com.client import Dispatch
   >>> ie = Dispatch("InternetExplorer.Application")
   >>> ie.Visible = 1    #Shows you the window so you can see what's 
happening.
  >>> ie.Navigate("https://www.suretrade.com/cgi-bin/login")  #The URL 
(secure or otherwise) you want to get to, this one used as an example 
is my online broker's secure URL.
  ... Well, it shows up in the browser... But how to get the
  text?
  "Tools/COM Browser"
     "Registered Type Libraries"
        "Microsoft Internet Controls"
            "Type Library"
                "IWebBrowser2"
  Hmmm. That seems to show a bit more info, but we still don't
  know what to do with it.. Let's go to msdn and see if we can
  look that control up... Here it is:
http://msdn.microsoft.com/workshop/browser/webbrowser/reference/ifaces/I
WebBrowser2/IWebBrowser2.asp
4) Looks like we want the "get Document" thing. Back to python.
>>> ie.Document
<COMObject Document>
Cool!
>>> doc = ie.Document
According to the MSDN docs, this is just a normal DHTML document
object.  For a list of controls and things you can do with this go to:
http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_do
cument.asp
>>> doc.url
'https://www.suretrade.com/cgi-bin/login'
>>> print doc.body.innerHTML
[it works!]
doesn't print the WHOLE HTML document, granted, just the body..  but
you can basically script it as if it were client side DHTML because
you're using the same engine. You may not need to grab it..  Huh.
5) Now to enter the data into the required fields:
>>> doc.LoginForm.UserID.value="Jabba"
A few notes here.  LoginForm is the NAME of the form, UserID is the 
NAME of the INPUT field in the form, value is the actual value in the 
field.  Flip to you browser and you'll see the text magically appear in 
the input fields in the browser.  To get the values from the field just 
refer to it:
>>> doc.LoginForm.UserID.value
'Jabba'
>>> doc.LoginForm.Passwd.value="password" (what else?)
>>> doc.LoginForm.destination.value = "/cgi-bin/order?action=Stock&"
This just sets an optional value in the dropdown box.
6) Now submit the form data:
>>> doc.LoginForm.submit()
And you're at the next screen, all logged on and ready to party!
7) Finally, to get rid of it:
>>> del doc      # Kills the object.
>>> ie.Quit()    # Kills the browser session.
You'll have to put in checks to verify whether the page is fully loaded 
(i.e. if doc.readyState == 'complete':) before submitting data and so 
on to make it more robust, but as a way of actually getting connected 
with results I think this should help anyone get started.
HTH any other newbies to Python/COM/web programming/etc (like me).