Changeset 4372 for trunk/GSASIIIO.py


Ignore:
Timestamp:
Mar 18, 2020 5:25:22 PM (20 months ago)
Author:
toby
Message:

streamline Post access to web pages; try http before https

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIIO.py

    r4365 r4372  
    22272227    return Layer
    22282228
     2229def postURL(URL,postdict):
     2230    '''Posts a set of values as from a web form. If access fails to an http
     2231    site the access is retried with https
     2232    :param str URL: the URL to post; typically something
     2233       like 'http://www.../dir/page?'
     2234    :param dict postdict: contains keywords and values, such
     2235       as {'centrosymmetry': '0', 'crystalsystem': '0', ...}
     2236    :returns: a string with the response from the web server or None
     2237       if access fails.
     2238    '''
     2239    try:
     2240        import requests # delay this until now, since rarely needed
     2241    except:
     2242        # this import seems to fail with the Anaconda pythonw on
     2243        # macs; it should not!
     2244        print('Warning: failed to import requests. Python config error')
     2245        return None
     2246       
     2247    repeat = True
     2248    while repeat:
     2249        r = None
     2250        repeat = False
     2251        try:
     2252            r = requests.get(URL,params=postdict)
     2253            if r.status_code == 200:
     2254                print('request OK')
     2255                page = r.text
     2256                return page # success
     2257            else:
     2258                print('request to {} failed. Reason={}'.format(URL,r.reason))
     2259        except Exception as msg:     #ConnectionError?
     2260            print('connection error - not on internet?')
     2261            if GSASIIpath.GetConfigValue('debug'): print(msg)
     2262        finally:
     2263            if r: r.close()
     2264        if URL.startswith('http:'):
     2265            repeat = True
     2266            URL = URL.replace('http:','https:')
     2267    else:
     2268        return None
     2269
    22292270if __name__ == '__main__':
    22302271    import GSASIIdataGUI
Note: See TracChangeset for help on using the changeset viewer.