PHP Classes

File: test/py/test2.py

Recommend this page to a friend!
  Packages of Nikos M.   Eazy HTTP   test/py/test2.py   Download  
File: test/py/test2.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: 1,611 bytes
 

Contents

Class file image Download
# -*- coding: utf-8 -*- 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): return EazyHttp().option('methods', [do_http]).get(uri) def test(): file_put_contents(DIR+'/test2-urllib.py.html', request('urllib', 'https://github.com/foo123/EazyHttp')['content']) file_put_contents(DIR+'/test2-socket.py.html', request('socket', 'https://github.com/foo123/EazyHttp')['content']) test()