File: web.py

package info (click to toggle)
psychopy 2020.2.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 44,056 kB
  • sloc: python: 119,649; javascript: 3,022; makefile: 148; sh: 125; xml: 9
file content (304 lines) | stat: -rwxr-xr-x 10,742 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Library for working with internet connections"""

# Part of the PsychoPy library
# Copyright (C) 2002-2018 Jonathan Peirce (C) 2019-2020 Open Science Tools Ltd.
# Distributed under the terms of the GNU General Public License (GPL).

from __future__ import absolute_import, print_function

# from future import standard_library
# standard_library.install_aliases()

from builtins import object
import sys
import socket
import re
from psychopy import logging
from psychopy.constants import PSYCHOPY_USERAGENT, PY3
from psychopy import prefs


if PY3:
    import urllib.request
    import urllib.error
    import urllib.parse
else:
    import urllib2
    # import urllib.request, urllib.error, urllib.parse

    class FakeURLlib(object):

        def __init__(self, lib):
            self.request = lib
            self.error = lib
            self.parse = lib
    urllib = FakeURLlib(urllib2)

# default 20s from prefs, min 2s
TIMEOUT = max(prefs.connections['timeout'], 2.0)
socket.setdefaulttimeout(TIMEOUT)

# global proxies
proxies = None  # if this is populated then it has been set up already


class NoInternetAccessError(Exception):
    """An internet connection is required but not available
    """
# global haveInternet
haveInternet = None  # gets set True or False when you check


def haveInternetAccess(forceCheck=False):
    """Detect active internet connection or fail quickly.

    If forceCheck is False, will rely on a cached value if possible.
    """
    global haveInternet
    if forceCheck or haveInternet is None:
        # try to connect to a high-availability site
        sites = ["http://www.google.com/", "http://www.opendns.com/"]
        for wait in [0.3, 0.7]:  # try to be quick first
            for site in sites:
                try:
                    urllib.request.urlopen(site, timeout=wait)
                    haveInternet = True  # cache
                    return True  # one success is good enough
                except Exception:  # urllib.error.URLError:
                    #  socket.timeout() can also happen
                    pass
        else:
            haveInternet = False
    return haveInternet


def requireInternetAccess(forceCheck=False):
    """Checks for access to the internet, raise error if no access.
    """
    if not haveInternetAccess(forceCheck=forceCheck):
        msg = 'Internet access required but not detected.'
        logging.error(msg)
        raise NoInternetAccessError(msg)
    return True


def tryProxy(handler, URL=None):
    """
    Test whether we can connect to a URL with the current proxy settings.

    `handler` can be typically `web.proxies`, if `web.setupProxy()` has been
    run.

    :Returns:

        - True (success)
        - a `urllib.error.URLError` (which can be interrogated with `.reason`)
        - a `urllib.error.HTTPError` (which can be interrogated with `.code`)

    """
    if URL is None:
        URL = 'http://www.google.com'  # hopefully google isn't down!
    req = urllib.request.Request(URL)
    opener = urllib.request.build_opener(handler)
    try:
        opener.open(req, timeout=2).read(5)  # open and read a few characters
        return True
    except urllib.error.URLError as err:
        return err
    except urllib.error.HTTPError as err:
        return err


def getPacFiles():
    """Return a list of possible auto proxy .pac files being used,
    based on the system registry (win32) or system preferences (OSX).
    """
    pacFiles = []
    if sys.platform == 'win32':
        try:
            import _winreg as winreg  # used from python 2.0-2.6
        except ImportError:
            import winreg  # used from python 2.7 onwards
        net = winreg.OpenKey(
            winreg.HKEY_CURRENT_USER,
            "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")
        nSubs, nVals, lastMod = winreg.QueryInfoKey(net)
        subkeys = {}
        for i in range(nVals):
            thisName, thisVal, thisType = winreg.EnumValue(net, i)
            subkeys[thisName] = thisVal
        if ('AutoConfigURL' in list(subkeys.keys()) and
                len(subkeys['AutoConfigURL']) > 0):
            pacFiles.append(subkeys['AutoConfigURL'])
    elif sys.platform == 'darwin':
        import plistlib
        sysPrefs = plistlib.readPlist('/Library/Preferences/System'
                                      'Configuration/preferences.plist')
        networks = sysPrefs['NetworkServices']
        # loop through each possible network (e.g. Ethernet, Airport...)
        for network in list(networks.items()):
            netKey, network = network  # the first part is a long identifier
            if 'ProxyAutoConfigURLString' in network['Proxies']:
                pacFiles.append(network['Proxies']['ProxyAutoConfigURLString'])
    return list(set(pacFiles))  # remove redundant ones


def getWpadFiles():
    """Return possible pac file locations from the standard set of .wpad
    locations

    NB this method only uses the DNS method to search, not DHCP queries, and
    so may not find all possible .pac locations.

    See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol
    """
    # pacURLs.append("http://webproxy."+domain+"/wpad.dat")
    # for me finds a file that starts: function FindProxyForURL(url,host)
    # dynamcially chooses a proxy based on the requested url and host; how to
    # parse?

    domainParts = socket.gethostname().split('.')
    pacURLs = []
    for ii in range(len(domainParts)):
        domain = '.'.join(domainParts[ii:])
        pacURLs.append("http://wpad." + domain + "/wpad.dat")
    return list(set(pacURLs))  # remove redundant ones


def proxyFromPacFiles(pacURLs=None, URL=None, log=True):
    """Attempts to locate and setup a valid proxy server from pac file URLs

    :Parameters:

        - pacURLs : list

            List of locations (URLs) to look for a pac file. This might
            come from :func:`~psychopy.web.getPacFiles` or
            :func:`~psychopy.web.getWpadFiles`.

        - URL : string

            The URL to use when testing the potential proxies within the files

    :Returns:

        - A urllib.request.ProxyHandler if successful (and this will have
          been added as an opener to the urllib)
        - False if no proxy was found in the files that allowed successful
          connection
    """

    if pacURLs == None:  # if given none try to find some
        pacURLs = getPacFiles()
    if pacURLs == []:  # if still empty search for wpad files
        pacURLs = getWpadFiles()
        # for each file search for valid urls and test them as proxies
    for thisPacURL in pacURLs:
        if log:
            msg = 'proxyFromPacFiles is searching file:\n  %s'
            logging.debug(msg % thisPacURL)
        try:
            response = urllib.request.urlopen(thisPacURL, timeout=2)
        except urllib.error.URLError:
            if log:
                logging.debug("Failed to find PAC URL '%s' " % thisPacURL)
            continue
        pacStr = response.read().decode('utf-8')
        # find the candidate PROXY strings (valid URLS), numeric and
        # non-numeric:
        pattern = r"PROXY\s([^\s;,:]+:[0-9]{1,5})[^0-9]"
        possProxies = re.findall(pattern, pacStr + '\n')
        for thisPoss in possProxies:
            proxUrl = 'http://' + thisPoss
            handler = urllib.request.ProxyHandler({'http': proxUrl})
            if tryProxy(handler) == True:
                if log:
                    logging.debug('successfully loaded: %s' % proxUrl)
                opener = urllib.request.build_opener(handler)
                urllib.request.install_opener(opener)
                return handler
    return False


def setupProxy(log=True):
    """Set up the urllib proxy if possible.

     The function will use the following methods in order to try and
     determine proxies:
        #. standard urllib.request.urlopen (which will use any
           statically-defined http-proxy settings)
        #. previous stored proxy address (in prefs)
        #. proxy.pac files if these have been added to system settings
        #. auto-detect proxy settings (WPAD technology)

     .. note:
        This can take time, as each failed attempt to set up a proxy
        involves trying to load a URL and timing out. Best
        to do in a separate thread.

    :Returns:

        True (success) or False (failure)
    """
    global proxies
    # try doing nothing
    proxies = urllib.request.ProxyHandler(urllib.request.getproxies())
    if tryProxy(proxies) is True:
        if log:
            logging.debug("Using standard urllib (static proxy or "
                          "no proxy required)")
        # this will now be used globally for ALL urllib opening
        urllib.request.install_opener(urllib.request.build_opener(proxies))
        return 1

    # try doing what we did last time
    if len(prefs.connections['proxy']) > 0:
        proxConnPref = {'http': prefs.connections['proxy']}
        proxies = urllib.request.ProxyHandler(proxConnPref)
        if tryProxy(proxies) is True:
            if log:
                msg = 'Using %s (from prefs)'
                logging.debug(msg % prefs.connections['proxy'])
            # this will now be used globally for ALL urllib opening
            opener = urllib.request.build_opener(proxies)
            urllib.request.install_opener(opener)
            return 1
        else:
            if log:
                logging.debug("Found a previous proxy but it didn't work")

    # try finding/using a proxy.pac file
    pacURLs = getPacFiles()
    if log:
        logging.debug("Found proxy PAC files: %s" % pacURLs)
    proxies = proxyFromPacFiles(pacURLs)  # installs opener, if successful
    if (proxies and
            hasattr(proxies, 'proxies') and
            len(proxies.proxies['http']) > 0):
        # save that proxy for future
        prefs.connections['proxy'] = proxies.proxies['http']
        prefs.saveUserPrefs()
        if log:
            msg = 'Using %s (from proxy PAC file)'
            logging.debug(msg % prefs.connections['proxy'])
        return 1

    # try finding/using 'auto-detect proxy'
    pacURLs = getWpadFiles()
    proxies = proxyFromPacFiles(pacURLs)  # installs opener, if successful
    if (proxies and
            hasattr(proxies, 'proxies') and
            len(proxies.proxies['http']) > 0):
        # save that proxy for future
        prefs.connections['proxy'] = proxies.proxies['http']
        prefs.saveUserPrefs()
        if log:
            msg = 'Using %s (from proxy auto-detect)'
            logging.debug(msg % prefs.connections['proxy'])
        return 1

    proxies = 0
    return 0