E5Network/E5NetworkProxyFactory.py

Fri, 31 Dec 2010 15:50:33 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 31 Dec 2010 15:50:33 +0100
branch
5_0_x
changeset 792
a13346916170
parent 320
340a978859c6
permissions
-rw-r--r--

Updated copyright notice.

270
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
792
a13346916170 Updated copyright notice.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 320
diff changeset
3 # Copyright (c) 2010 - 2011 Detlev Offenbach <detlev@die-offenbachs.de>
270
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a network proxy factory.
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import sys
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import os
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 from PyQt4.QtCore import QUrl
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 from PyQt4.QtGui import QMessageBox
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 from PyQt4.QtNetwork import QNetworkProxyFactory, QNetworkProxy, QNetworkProxyQuery
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 import Preferences
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 class E5NetworkProxyFactory(QNetworkProxyFactory):
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 Class implementing a network proxy factory.
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 def __init__(self):
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 Constructor
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 QNetworkProxyFactory.__init__(self)
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 def queryProxy(self, query):
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 Public method to determine a proxy for a given query.
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @param query reference to the query object (QNetworkProxyQuery)
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 @return list of proxies in order of preference (list of QNetworkProxy)
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 """
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 if query.queryType() == QNetworkProxyQuery.UrlRequest and \
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 query.protocolTag() in ["http", "https", "ftp"] and \
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 Preferences.getUI("UseProxy"):
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 if Preferences.getUI("UseSystemProxy"):
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 proxyList = QNetworkProxyFactory.systemProxyForQuery(query)
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 if sys.platform not in ["darwin", "nt"] and \
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 len(proxyList) == 1 and \
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 proxyList[0].type() == QNetworkProxy.NoProxy:
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 # try it the Python way
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 # scan the environment for variables named <scheme>_proxy
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 # scan over whole environment to make this case insensitive
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 for name, value in os.environ.items():
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 name = name.lower()
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 if value and name[-6:] == '_proxy' and \
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 name[:-6] == query.protocolTag().lower():
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 url = QUrl(value)
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 if url.scheme() in ["http", "https"]:
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 proxyType = QNetworkProxy.HttpProxy
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 else:
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 proxyType = QNetworkProxy.FtpCachingProxy
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 proxy = QNetworkProxy(proxyType, url.host(), url.port(),
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 url.userName(), url.password())
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 proxyList = [proxy]
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 break
318
e513e6d31858 Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 282
diff changeset
60 if proxyList:
e513e6d31858 Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 282
diff changeset
61 proxyList[0].setUser(Preferences.getUI("ProxyUser"))
e513e6d31858 Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 282
diff changeset
62 proxyList[0].setPassword(Preferences.getUI("ProxyPassword"))
320
340a978859c6 Fixed a few issues in the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 318
diff changeset
63 return proxyList
340a978859c6 Fixed a few issues in the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 318
diff changeset
64 else:
340a978859c6 Fixed a few issues in the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 318
diff changeset
65 return [QNetworkProxy(QNetworkProxy.NoProxy)]
270
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 else:
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 host = Preferences.getUI("ProxyHost")
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 if not host:
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 QMessageBox.critical(None,
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 self.trUtf8("Proxy Configuration Error"),
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 self.trUtf8("""Proxy usage was activated"""
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 """ but no proxy host configured."""))
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 return [QNetworkProxy(QNetworkProxy.DefaultProxy)]
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 else:
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 pProxyType = Preferences.getUI("ProxyType")
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 if pProxyType == 0:
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 proxyType = QNetworkProxy.HttpProxy
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 elif pProxyType == 1:
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 proxyType = QNetworkProxy.HttpCachingProxy
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 elif pProxyType == 2:
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 proxyType = QNetworkProxy.Socks5Proxy
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 proxy = QNetworkProxy(proxyType, host,
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 Preferences.getUI("ProxyPort"),
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 Preferences.getUI("ProxyUser"),
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 Preferences.getUI("ProxyPassword"))
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 return [proxy, QNetworkProxy(QNetworkProxy.DefaultProxy)]
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 else:
41505c92ac31 Added code to enhance the proxy configuration and removed the usage of QHttp.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 return [QNetworkProxy(QNetworkProxy.NoProxy)]

eric ide

mercurial