PHP Classes

File: test/py/test-redirect.py

Recommend this page to a friend!
  Packages of Nikos M.   Eazy HTTP   test/py/test-redirect.py   Download  
File: test/py/test-redirect.py
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Eazy HTTP
Send HTTP requests defined with a fluent interface
Author: By
Last change: v.1.2.0

* python socket implementation
* various edits, corrections
Date: 4 months ago
Size: 2,481 bytes
 

Contents

Class file image Download
# -*- coding: utf-8 -*- # run "php -S 127.0.0.1:9000 test-server.php" import os, sys DIR = os.path.dirname(os.path.abspath(__file__)) def import_module(name, path): #import imp #try: # mod_fp, mod_path, mod_desc = imp.find_module(name, [path]) # mod = getattr( imp.load_module(name, mod_fp, mod_path, mod_desc), name ) #except ImportError as exc: # mod = None # sys.stderr.write("Error: failed to import module ({})".format(exc)) #finally: # if mod_fp: mod_fp.close() #return mod import importlib.util, sys spec = importlib.util.spec_from_file_location(name, path+name+'.py') mod = importlib.util.module_from_spec(spec) sys.modules[name] = mod spec.loader.exec_module(mod) return getattr(mod, name) # import the EazyHttp.py (as a) module, probably you will want to place this in another dir/package EazyHttp = import_module('EazyHttp', os.path.join(DIR, '../../src/py/')) if not EazyHttp: print ('Could not load the EazyHttp Module') sys.exit(1) def file_put_contents(path, content): if isinstance(content, bytes): with open(path, "wb") as file: file.write(content) else: with open(path, "w") as file: file.write(str(content)) def request(do_http, uri, follow_redirects): return EazyHttp().option('methods', [do_http]).option('follow_redirects', follow_redirects).get('http://127.0.0.1:9000' + uri) def test(): import json try: response = request('urllib', '/test/redirect.php?max_redirects=2', 3) file_put_contents(DIR+'/test-redirect-urllib.php.txt', json.dumps(response)) except Exception as error: print(str(error)) try: response = request('socket', '/test/redirect.php?max_redirects=2', 3) file_put_contents(DIR+'/test-redirect-socket.php.txt', json.dumps(response)) except Exception as error: print(str(error)) try: response = request('urllib', '/test/redirect.php?max_redirects=10', 3) file_put_contents(DIR+'/test-redirect-max-urllib.php.txt', json.dumps(response)) except Exception as error: print(str(error)) try: response = request('socket', '/test/redirect.php?max_redirects=10', 3) file_put_contents(DIR+'/test-redirect-max-socket.php.txt', json.dumps(response)) except Exception as error: print(str(error)) test()