Changeset 4372


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

streamline Post access to web pages; try http before https

Location:
trunk
Files:
3 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
  • trunk/SUBGROUPS.py

    r4365 r4372  
    1616########### SVN repository information ###################
    1717from __future__ import division, print_function
    18 import requests
    1918import numpy as np
    2019import numpy.linalg as nl
    2120import GSASIIspc as G2spc
     21import GSASIIIO as G2IO
    2222import GSASIIpath
    2323GSASIIpath.SetBinaryPath()
    24 submagSite = 'https://www.cryst.ehu.es/cgi-bin/cryst/programs/subgrmag1_general_GSAS.pl?'
     24submagSite = 'http://www.cryst.ehu.es/cgi-bin/cryst/programs/subgrmag1_general_GSAS.pl?'
    2525
    2626def GetNonStdSubgroups(SGData, kvec,star=False,landau=False,maximal=False):
     
    8383        for i,k in zip(('x','y','z'),kvec[3*j-3:3*j]):
    8484            postdict['knm%d%s'%(j,i)] = k
    85     try:
    86         r = requests.get(submagSite,params=postdict)
    87     except:     #ConnectionError?
    88         page = ''
    89         print('connection error - not on internet')
     85    page = G2IO.postURL(submagSite,postdict)
     86    if not page:
     87        print('connection error - not on internet?')
    9088        return None,None
    91     if r.status_code == 200:
    92         print('request OK')
    93         page = r.text
    94         page = page.replace('<font style= "text-decoration: overline;">','<font>-')
    95     else:
    96         page = ''
    97         print('request failed. Reason=',r.reason)
    98         return None,None
    99     r.close()
    100    
     89    page = page.replace('<font style= "text-decoration: overline;">','<font>-')
    10190    result = page.replace('&','\n')
    10291    result = result.split('\n')
     
    211200        for i,k in zip(('x','y','z'),kvec[3*j-3:3*j]):
    212201            postdict['km%d%s'%(j,i)] = k
    213     try:
    214         r = requests.get(submagSite,params=postdict)
    215     except:     #ConnectionError?
    216         page = ''
    217         print('connection error - not on internet')
     202    page = G2IO.postURL(submagSite,postdict)
     203    if not page:
     204        print('connection error - not on internet?')
    218205        return None,None
    219     if r.status_code == 200:
    220         print('request OK')
    221         page = r.text
    222         page = page.replace('<font style= "text-decoration: overline;">','<font>-')
    223     else:
    224         page = ''
    225         print('request failed. Reason=',r.reason)
    226         return None,None
    227     r.close()
    228 
     206    page = page.replace('<font style= "text-decoration: overline;">','<font>-')
    229207    result = page.replace('&','\n')
    230208    result = result.split('\n')
     
    283261    datastr = "sgr={:}&cell={:}&tol={:}&submit=Show".format(
    284262        str(int(spgNum)),cellstr,str(int(tol)))
    285     try:
    286         r = requests.get(psSite,params=datastr)
    287     except:     #ConnectionError?
    288         page = ''
    289         print('connection error - not on internet')
     263    page = G2IO.postURL(psSite,datastr)
     264    if not page:
     265        print('connection error - not on internet?')
    290266        return None
    291     if r.status_code == 200:
    292         print('request OK')
    293         page = r.text
    294         page = page.replace('<font style= "text-decoration: overline;">','<font>-')
    295     else:
    296         page = ''
    297         print('request failed. Reason=',r.reason)
    298         return None
    299     r.close()
     267    page = page.replace('<font style= "text-decoration: overline;">','<font>-')
    300268    return page
    301269
  • trunk/kSUBGROUPSMAG.py

    r4365 r4372  
    1616########### SVN repository information ###################
    1717from __future__ import division, print_function
    18 import requests
    1918try:
    2019    import HTMLParser as HTML
     
    2221    import html.parser as HTML # Python 3
    2322import GSASIIspc as G2spc
     23import GSASIIIO as G2IO
    2424import GSASIIpath
    2525GSASIIpath.SetBinaryPath()
     
    140140        for i,k in zip(('x','y','z'),kvec[3*j-3:3*j]):
    141141            postdict['km%d%s'%(j,i)] = k
    142     try:
    143         r = requests.get(submagSite,params=postdict)
    144     except:     #ConnectionError?
    145         page = ''
    146         print('connection error - not on internet')
     142    page = G2IO.postURL(submagSite,postdict)
     143    if not page:
     144        print('connection error - not on internet?')
    147145        return None,None
    148     if r.status_code == 200:
    149         print('request OK')
    150         page = r.text
    151         page = page.replace('<font style= "text-decoration: overline;">','<font>-')
    152     else:
    153         page = ''
    154         print('request failed. Reason=',r.reason)
    155         return None,None
    156     r.close()
    157 
     146    page = page.replace('<font style= "text-decoration: overline;">','<font>-')
    158147    p = TableParser()
    159148    p.feed(page)
     
    213202        for i,k in zip(('x','y','z'),kvec[3*j-3:3*j]):
    214203            postdict['knm%d%s'%(j,i)] = k
    215     try:
    216         r = requests.get(submagSite,params=postdict)
    217     except:     #ConnectionError?
    218         page = ''
    219         print('connection error - not on internet')
    220         return None
    221     if r.status_code == 200:
    222         print('request OK')
    223         page = r.text
    224         page = page.replace('<font style= "text-decoration: overline;">','<font>-')
    225     else:
    226         page = ''
    227         print('request failed. Reason=',r.reason)
    228         return None
    229     r.close()
    230 
     204    if not page:
     205        print('connection error - not on internet?')
     206        return None,None
     207    page = page.replace('<font style= "text-decoration: overline;">','<font>-')
    231208    p = TableParser()
    232209    p.feed(page)
Note: See TracChangeset for help on using the changeset viewer.