IE8 automation and https - python

I'm trying to use IE8 through COM to access a secured site (namely, SourceForge), in Python. Here is the script:
from win32com.client import gencache
from win32com.client import Dispatch
import pythoncom
gencache.EnsureModule('{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}', 0, 1, 1)
class SourceForge(object):
def __init__(self, baseURL='https://sourceforget.net/', *args, **kwargs):
super(SourceForge, self).__init__(*args, **kwargs)
self.__browser = Dispatch('InternetExplorer.Application')
self.__browser.Visible = True
self.__browser.Navigate(baseURL)
def run(self):
while True:
pythoncom.PumpMessages()
def main():
sf = SourceForge()
sf.run()
if __name__ == '__main__':
main()
If I launch IE by hand, fine. If I launch the script, I get a generic error page "Internet Explorer cannot display this page". If I change baseURL to use http instead of https, the script works. I guess this is some security "feature". I tried adding the site to the list of trusted sites. I tried to enable IE scripting in the options for the Internet zone. Doesn't work. Google was no help.
So, does anybody know something about this ? Is there a mysterious option to enable or am I doomed ?
I'm on Windows XP SP3 BTW, Python 2.5 and pywin32 build 213.

I can't open https://sourceforget.net/ -- not by hand, not by script.
Are you sure this link is right?

Related

Specifying local Chromedriver path for Dash application testing with webdriver-manager

I am trying to leverage testing Dash application as described here: https://dash.plotly.com/testing
However I found no way of specifying the Chromedriver path for the webdriver-manager under the hood of dash testing.
I tried this below which calls webdriver-manager before reaching the test code:
def test_bsly001_falsy_child(dash_duo):
app = import_app("my_app_path")
dash_duo.start_server(app)
webdriver-manager then would start downloading the latest Chrome version. But due to company policy we cannot just download things from the internet, it is blocked by firewall. We are supposed to use the Chromedriver which is already downloaded for us on the internal network.
I tried implementing a pytest fixture to set up the Chrome driver before the testing starts:
driver = webdriver.Chrome(executable_path="...")
But webdriver-manager does not accept this.
Do you know any ways of working around this? Any hints?
Any way of doing Dash testing without webdriver-manager?
Thanks.
I had a similar problem and ended up with another pytest fixture dash_duo_bis which uses another class DashComposite(Browser) inside the conftest.py where the _get_chrome method is overriden as follows:
class DashComposite(Browser):
def __init__(self, server, **kwargs):
super().__init__(**kwargs)
self.server = server
def get_webdriver(self):
return self._get_chrome()
def _get_chrome(self):
return webdriver.Chrome(executable_path = r"MY_CHROME_EXECUTABLE_PATH")
def start_server(self, app, **kwargs):
"""Start the local server with app."""
# start server with app and pass Dash arguments
self.server(app, **kwargs)
# set the default server_url, it implicitly call wait_for_page
self.server_url = self.server.url

Use Python to manage content in "Enterprise Architect" software from "sparx systems"

Currently i am working with enterprise architect software for creating packages, diagrams.
Is it possible to work in Enterprise architect software using python script ? Some example like Deleting and creating packages and diagrams etc. If so refer example code or link.
Sure, that's no problem.
import win32com.client
from singleton import Singleton
#Singleton
class Repository:
def __init__(self):
try:
self.eaRep = win32com.client.Dispatch("EA.App").Repository
models = self.eaRep.models
done = True
except Exception as e:
print (e)
done = False
(The #Singleton can be found on the net but you can work without it.)
Then in your main program you can access the repository like
rep = repository.Repository.Instance()
print rep.modules.getAt(0).name
etc. Have fun
import win32com.client
def open_repository(path, login, password):
eaApp = win32com.client.Dispatch("EA.App")
eaRep = eaApp.Repository
if login:
eaRep.SuppressSecurityDialog = True
eaRep.OpenFile2(path, login, password)
else:
eaRep.OpenFile(path)
return eaRep
please use OpenFile for open your model. (OpenFile2 if your model has enabled security)

Sublime Text 3: Simple plug-in that changes color theme depending on remote host

Setup: I use Sublime Text 3 (ST), and I often have 2-3 different sessions with Sublime + iTerm2 open in different remote workspaces using RemoteSubl.
Using a simple batch script, I have set my iTerm2 to change colours (by activating a different iTerm user) when I ssh into a different host.
I was wondering if the same could be done for RemoteSubl? Such that when I open something from a specific host/ip/port, then Sublime opens in a different colour scheme, depending on the host/ip/port.
Solution attempt: So far, this is my attempt at building a small plugin that changes colour scheme when host is remote_host.
import sublime
import sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, view):
try:
host = view.settings().get('remote_subl.host')
print(host)
if host == 'remote_host':
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Mariana.tmTheme')
print(view.settings().get('color_scheme'))
except:
print("Not on remote_host")
pass
Problem: When using using view.settings().get('remote_subl.host') in the console it works fine, and returns remote_host. However, when running the script view.run_command('example') I get the "Not on remote_host" print, indicating that the try loop fails for some reason.
After Keiths suggestions:
import sublime
import sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, view):
view = self.view
host = view.settings().get('remote_subl.host', None)
print(host)
if host:
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Mariana.tmTheme')
print(view.settings().get('color_scheme'))
if host is None:
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Monokai.tmTheme')
print(view.settings().get('color_scheme'))
view isn't an argument that is passed to the TextCommand's run method. Instead, it is a property on self. Changing it to the following should work:
import sublime
import sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
try:
host = view.settings().get('remote_subl.host')
print(host)
if host == 'dsintmain':
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Mariana.tmTheme')
print(view.settings().get('color_scheme'))
except:
print("Not on remote_host")
pass
I would also recommend printing the exception that occurs to help debug things like this in future. Even better, rather than expecting an exception in normal usage, provide a default value to the get method on the settings (i.e. None) and remove the exception handling altogether.
host = view.settings().get('remote_subl.host', None)
That way, if something does really go wrong, you'll see the traceback in the ST console.

mitmproxy load script using API (Python)

Good day,
I am trying to implement the mitmproxy into a bigger application.
For that, I need to be able to load those so called inline scripts in my code and not via command line. I could not find any helpful information about that in the documentation.
I am using mitmproxy version 0.17 and Python 2.7.
I know there is a newer version available, but that one didnt worked using the code examples.
This is the base code I have:
from mitmproxy import controller, proxy
from mitmproxy.proxy.server import ProxyServer
class ProxyMaster(controller.Master):
def __init__(self, server):
controller.Master.__init__(self, server)
def run(self):
try:
return controller.Master.run(self)
except KeyboardInterrupt:
self.shutdown()
def handle_request(self, flow):
flow.reply()
def handle_response(self, flow):
flow.reply()
config = proxy.ProxyConfig(port=8080)
server = ProxyServer(config)
m = ProxyMaster(server)
m.run()
How could I run this proxy using inline scripts?
Thanks in advance
I figured myself out a really ugly workaround.
Instead of using controller.Master I had to use flow.FlowMaster as the controller.Master lib does not seem to be able to handle inline scripts.
For some reason just loading the files did not work, they get triggered immediately, but not by running their matching hooks.
Instead of using the hooks which are not working, I am loading the matching functions as you can see in handle_response (try/except is missing and threading could be useful)
from mitmproxy import flow, proxy
from mitmproxy.proxy.server import ProxyServer
import imp
class ProxyMaster(flow.FlowMaster):
def run(self):
try:
return flow.FlowMaster.run(self)
except KeyboardInterrupt:
self.shutdown()
def handle_request(self, flow):
flow.reply()
def handle_response(self, flow):
for inline_script in self.scripts:
script_file = imp.load_source("response", inline_script.filename)
script_file.response(self, flow)
flow.reply()
proxy_config = proxy.ProxyConfig(port=8080)
server = ProxyServer(proxy_config)
state = flow.State()
m = ProxyMaster(server, state)
m.load_script("upsidedowninternet.py")
m.load_script("add_header.py")
m.run()
Any ideas about doing it the right way are appreciated.

How to self-handling cookies in PyObjC

I'm implementing a minimal browser in PyObjC for my study.
First, I googled about the way to use webkit from pyobjc and wrote code like below:
#coding: utf-8
import Foundation
import WebKit
import AppKit
import objc
def main():
app = AppKit.NSApplication.sharedApplication()
rect = Foundation.NSMakeRect(100,350,600,800)
win = AppKit.NSWindow.alloc()
win.initWithContentRect_styleMask_backing_defer_(
rect,
AppKit.NSTitledWindowMask |
AppKit.NSClosableWindowMask |
AppKit.NSResizableWindowMask |
AppKit.NSMiniaturizableWindowMask,
AppKit.NSBackingStoreBuffered,
False)
win.display()
win.orderFrontRegardless()
webview = WebKit.WebView.alloc()
webview.initWithFrame_(rect)
pageurl = Foundation.NSURL.URLWithString_("http://twitter.com")
req = Foundation.NSURLRequest.requestWithURL_(pageurl)
webview.mainFrame().loadRequest_(req)
win.setContentView_(webview)
app.run()
if __name__ == '__main__':
main()
It worked fine. But I noticed that this browser is sharing cookies with safari. I want it to be independent from my Safari.app.
So I googled again and I learned that I can override cookie-handling-methods by using NSMutableURLRequest.
Below is the second code I tested:
#coding: utf-8
import Foundation
import WebKit
import AppKit
import objc
def main():
app = AppKit.NSApplication.sharedApplication()
rect = Foundation.NSMakeRect(100,350,600,800)
win = AppKit.NSWindow.alloc()
win.initWithContentRect_styleMask_backing_defer_(
rect,
AppKit.NSTitledWindowMask |
AppKit.NSClosableWindowMask |
AppKit.NSResizableWindowMask |
AppKit.NSMiniaturizableWindowMask,
AppKit.NSBackingStoreBuffered,
False)
win.display()
win.orderFrontRegardless()
webview = WebKit.WebView.alloc()
webview.initWithFrame_(rect)
pageurl = Foundation.NSURL.URLWithString_("http://twitter.com")
req = Foundation.NSMutableURLRequest.requestWithURL_(pageurl)
Foundation.NSMutableURLRequest.setHTTPShouldHandleCookies_(req, False)
webview.mainFrame().loadRequest_(req)
win.setContentView_(webview)
app.run()
if __name__ == '__main__':
main()
This code show me a login screen of twitter :-)
But I couldn't login to twitter by this browser.
I input account name, password and pushed enter key. Then the browser displays the timeline of the account which I always use in Safari.app.
Yes, I know that it's proper result.
I didn't write anything about handling cookies.
And my question is on this point.
I want to know that:
How can I implement and use something like NSHTTPCookieStorage?
Can I write it in python?
Thank you.
To start with the easy part: if it is possible to do this in Objective-C it should also be possible with PyObjC.
That said, it is unclear to me if this is possible at all. How can I have multiple instances of webkit without sharing cookies? seems to indicate that it isn't although you might be able to do something through the webkit delegate.
An other alternative is to use NSURLProtocol, register a custom NSURLProtocol class for handling http/https requests and implement that using Python's urllib or urllib2. The PyDocURL example shows how to do this (that example registers a subclass for pydoc:// URLs).
More information on NSURLConnection is on Apple's website.
Updated with an implemention hint:
An alternate method might be to disable cookie storaga by NSHTTPCookieStorage (NSHTTPCookieStorage.sharedHTTPCookieStorage.setCookieAcceptPolicy_(NSHTTPCookieAcceptPolicyNever)). Then use the webkit resource loading delegate to handle cookies yourself:
Maintain your own cookie store (possibly using a class in urllib2)
In webView:resource:willSendRequest:redirectResponse:fromDataSource: add cookie headers based on information in that store
In webView:resource:didReceiveResponse:fromDataSource: check for "set-cookie" headers and update your own cookie store.
It shouldn't be too hard to do this, and I'd love to have this functionality as an example on the PyObjC website (or even as a utility class in the WebKit bindings for PyObjC).

Categories