{{error}}
{{(quickSearchResults.length>10)?'10+':(quickSearchResults.length)}} {{(quickSearchResults.length==1)?'result':'results'}}
{{result.title}} {{result.timeStamp | mysql2ymd }}
I am sorry, no such article was written yet.
Postgres connection for Python with optional SSH tunnel
print(Postgresql('postgresql://postgres:postgres@host:5432/db_name','ssh://root:root_password@host:22').select('select %s + %s;', 1, 4)) print(Postgresql('postgresql://postgres:postgres@host:5432/db_name').select('select %s + %s;', 1, 4))
1 files attached: postgresql.py
postgresql.py
import urllib
from typing import Union
from urllib.parse import urlparse

import psycopg2 as psycopg2

from sorescu.ssh import SshTunnel


class Postgresql:
    def __init__(self, postgres_url: str, ssh_url: Union[str, None] = None):
        if postgres_url[0:13] == 'postgresql://':
            postgres_url = urlparse(postgres_url)
            if ssh_url is not None:
                tunnel = SshTunnel.SshTunnel(ssh_url, postgres_url.port)
                self._connection = psycopg2.connect(host=tunnel.local_bind_host, port=tunnel.local_bind_port,
                                                    user=postgres_url.username,
                                                    password=postgres_url.password, database=postgres_url.path[1:],
                                                    sslmode=
                                                    urllib.parse.parse_qs(postgres_url.query).get('sslmode', [None])[0])
            else:
                self._connection = psycopg2.connect(host=postgres_url.hostname, port=postgres_url.port,
                                                    user=postgres_url.username, password=postgres_url.password,
                                                    database=postgres_url.path[1:],
                                                    sslmode=
                                                    urllib.parse.parse_qs(postgres_url.query).get('sslmode', [None])[0])
        else:
            raise Exception(f"Postgresql URL not supported: `{postgres_url}`")
        self._connection.autocommit = True

    def exec(self, query: str, *vargs: object):
        cursor = self._connection.cursor()
        print(query)
        cursor.execute(query, tuple(vargs))
        return cursor

    def select(self, query: str, *vargs: object):
        cursor = self.exec(query, *vargs)
        rows = cursor.fetchall()
        columns = cursor.description
        return [({columns[idx].name: row[idx] for idx in range(len(columns))}) for row in rows]

    def append(self, table_name: str, data: dict):
        key_code = ','.join(['"' + key + '"' for key in data])
        query = f"INSERT INTO {table_name} ({key_code})VALUES ({','.join(['%s' for key in data])}); commit;"
        self.exec(query, *[data[key] for key in data])


if __name__ == '__main__':
    print(Postgresql('postgresql://postgres:postgres@host:5432/db_name','ssh://root:root_password@user:22').select('select %s + %s;', 1, 4))
    print(Postgresql('postgresql://postgres:postgres@localhost:5432/db_name').select('select %s + %s;', 1, 4))
Python SSH Tunnel Forwarder
Postgres forwarder: SshTunnel.SshTunnel("ssh://user:pass@host:port", 5432)
1 files attached: SshTunnel.py
SshTunnel.py
import functools
import urllib

from sshtunnel import SSHTunnelForwarder
from urllib.parse import urlparse
import socket
from contextlib import closing


def find_free_port():
    with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
        s.bind(('', 0))
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        return s.getsockname()[1]


class SshTunnel(SSHTunnelForwarder):
    _cache = dict()

    @functools.lru_cache
    def __init__(self, url: str, remote_port: int):
        url = urlparse(url)
        local_port = find_free_port()
        super().__init__((url.hostname, url.port),
                         ssh_username=urllib.parse.unquote(url.username),
                         ssh_password=urllib.parse.unquote(url.password),
                         remote_bind_address=('localhost', remote_port),
                         local_bind_address=('localhost', local_port)
                         )
        self.start()
        self._server_list[0].block_on_close = False
Python minimal HTTP server
1 files attached: server.py
server.py
#!/usr/bin/python
import functools
import http.server
import json
import os
import re
import sys
import traceback
import urllib
from typing import Union
import urllib.parse

class HttpRequest:
    __ROOT_FOLDER = os.path.join(Config().root_folder(), 'http')

    def __init__(self, handler: http.server.SimpleHTTPRequestHandler):
        self.__handler = handler
        self._url = urllib.parse.urlparse(self.__handler.path)

        segments = self._url.path[1::].split('/')

        self.file_path = self.__ROOT_FOLDER
        while segments.__len__() > 0:
            candidate = os.path.join(self.file_path, segments[0])
            if not os.path.exists(candidate):
                if not os.path.exists(f"{candidate}.py"):
                    break
            self.file_path = os.path.join(self.file_path, segments[0])
            segments = segments[1:]
        self.SEGMENTS={k: v for k, v in zip(range(0, segments.__len__()), segments)}

    # @property
    # @functools.lru_cache
    # def file_path(self):
    #     result = self._url.path[1::]
    #     segments = result.split('/')
    #     current_path = self.__ROOT_FOLDER
    #     while segments.__len__() > 0:
    #         candidate = os.path.join(current_path, segments[0])
    #         if not os.path.exists(candidate):
    #             if not os.path.exists(f"{candidate}.py"):
    #                 break
    #         current_path = os.path.join(current_path, segments[0])
    #         segments = segments[1:]
    #     return current_path

    @property
    @functools.lru_cache
    def file(self):
        # segments=self.file_path.split('/')
        # segments.remove(0)
        # print(f">>>>>>>>> {segments}")
        result = self.file_path
        print(f"file: {self.file_path}")
        if os.path.isfile(result):
            return result
        print(f"file2: {self.file_path}")
        if not os.path.exists(result):
            if os.path.exists(result + '.py'):
                print(f"file3: {self.file_path}")
                return result + '.py'
        print(f"file4: {self.file_path}")
        if os.path.isdir(result):
            print(f"file5: {self.file_path}")
            result = os.path.join(result, 'index.html')
        if os.path.exists(result):
            print(f"file6: {self.file_path}")
            return result
        raise FileNotFoundError(f"HTTP resource `{self.__handler.path}` not found: {result}")

    @property
    @functools.lru_cache
    def extension(self):
        return os.path.splitext(self.file)[1][1::]

    @property
    def GET(self) -> dict:
        qs = urllib.parse.parse_qs(self._url.query)
        return dict((key, qs[key][0]) for key in qs)

    @property
    @functools.lru_cache
    def text(self):
        if self.__handler.headers['Content-Type'] is not None:
            if self.__handler.headers['Content-Type'].startswith('application/json'):
                return self.__handler.rfile.read(int(self.__handler.headers['Content-Length']))
        return ''

    @property
    @functools.lru_cache
    def POST(self):
        if self.text != "" and self.text is not None:
            return json.loads(self.text)
        else:
            return dict()

    @property
    @functools.lru_cache
    def ARGS(self) -> dict:
        result = dict()
        result.update(self.SEGMENTS)
        result.update(self.GET)
        if isinstance(self.POST, dict):
            result.update(self.POST)
        return result


class HttpResponse:
    def __init__(self, handler: http.server.SimpleHTTPRequestHandler):
        self.__handler = handler
        self.code = 200
        self.body = bytearray()
        self.headers = {}

    def write(self, data: Union[str, bytes, BaseException]):
        if isinstance(data, str):
            self.body += bytes(data, 'utf-8')
        elif isinstance(data, bytes):
            self.body += data
        elif isinstance(data, BaseException):
            self.write('</pre></style></script></textarea><pre color="red">\r\n')
            self.write(data.__str__())
            self.write('\r\n\t')
            self.write('\r\n\t'.join(traceback.format_tb(data.__traceback__)))

            sys.stderr.write(data.__str__())
            sys.stderr.write('\r\n\t')
            sys.stderr.write('\r\n\t'.join(traceback.format_tb(data.__traceback__)))
        else:
            raise TypeError(f"Type not supported: {data.__class__.__qualname__}")


class WebHandler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._METHOD = None

    @property
    @functools.lru_cache
    def _REQUEST(self):
        return HttpRequest(self)

    @property
    @functools.lru_cache
    def _RESPONSE(self):
        return HttpResponse(self)

    def do_request(self, method):
        print(f"do_request {method}")
        self._METHOD = method
        try:
            open(self._REQUEST.file, "rb").read()
        except FileNotFoundError:
            print(f"File not found: {self._REQUEST.file_path}")
            self._RESPONSE.headers['Content-Type'] = 'text/html'
            self._RESPONSE.code = 404
            self._RESPONSE.write("<style>*{font-family:sans-serif}</style>")
            self._RESPONSE.write(f"<h1>{self.path}</h1>")
            self._RESPONSE.write(f"<a href='..'>Go up one level</a><br/>")

            if os.path.isdir(self._REQUEST.file_path):
                self._RESPONSE.write(f"<strong>This folder contains no index.html</strong><br/>")
            else:
                self._RESPONSE.write(f"<strong>This resource does not exist.</strong><br/>")
            self._RESPONSE.write("<br/>")
            if os.path.isdir(self._REQUEST.file_path):
                for segment in os.listdir(self._REQUEST.file_path):
                    if os.path.isdir(os.path.join(self._REQUEST.file_path, segment)):
                        segment = os.path.join(segment, '')
                    self._RESPONSE.write(f"<a href='{segment}'>{segment}</a><br/><br/>")
        try:
            if self._REQUEST.extension == 'py':
                exec(compile(open(self._REQUEST.file, "r").read(),self._REQUEST.file_path,'exec'))
            else:
                self._RESPONSE.body += open(self._REQUEST.file, "rb").read()
        except BaseException as exception:
            print(exception)
            self._RESPONSE.code = 422
            self._RESPONSE.write(exception)

        self.send_response(self._RESPONSE.code)
        try:
            body_text = str(self._RESPONSE.body, 'utf-8')
            body_text = re.sub(pattern=r"<%fa\:([^%]*)%>", repl='<span class="fas fa-\\1"></span>', string=body_text)
            self._RESPONSE.body = bytes(body_text, 'utf-8')
        except:
            pass
        self._RESPONSE.headers['Content-length'] = self._RESPONSE.body.__len__()
        for k in self._RESPONSE.headers:
            print(k + ": " + self._RESPONSE.headers[k].__str__())
            self.send_header(k, self._RESPONSE.headers[k].__str__())
        self.end_headers()
        self.wfile.write(self._RESPONSE.body)

    def do_GET(self):
        self.do_request('GET')

    def do_PUT(self):
        self.do_request('PUT')

    def do_DELETE(self):
        self.do_request('DELETE')

    def do_POST(self):
        self.do_request('POST')


class HttpServer:
    def __init__(self, port: int = 3187):
        self._server = http.server.ThreadingHTTPServer
        self._httpd = self._server(("", port), WebHandler)

    def start(self):
        print("HttpServer.start()")
        self._httpd.serve_forever()


if __name__ == "__main__":
    HttpServer().start()
Postgresql remote query in Java when direct or SSL is not working
2 files attached: SshPostgresql.java postgresql-batch.py
SshPostgresql.java postgresql-batch.py
import psycopg2, sys, json, datetime


def deserialize(v):
    if isinstance(v, dict):
        if '$datetime.datetime' in v.keys():
            return datetime.datetime(v['$datetime.datetime'])
        elif '$datetime.timedelta'in v.keys():
            return datetime.timedelta(v['$datetime.timedelta'])
        else:
            raise Exception('Type not understood: '+v.__str__())
    return v
def serialize(v):
    if (isinstance(v, datetime.datetime)):
        epochZero = datetime.datetime(1970, 1, 1, tzinfo=v.tzinfo)
        return {'$datetime.datetime': (v - epochZero).total_seconds()}
    if (isinstance(v, datetime.timedelta)):
        return {'$datetime.timedelta': v.total_seconds()}
    return v


batchResult = []
for request in json.load(open(sys.argv[1], 'r')):
    cursor = psycopg2.connect(host=sys.argv[2], port=sys.argv[3], user=sys.argv[4], password=sys.argv[5], database=sys.argv[6]).cursor()
    cursor.execute(request['query'], tuple([deserialize(v)for v in request['parameters']]))
    try:
        rows = cursor.fetchall()
        columns = cursor.description
        rs = [({columns[idx].name: serialize(row[idx]) for idx in range(len(columns))}) for row in rows]
        batchResult.append(rs)
    except psycopg2.ProgrammingError, e:
        batchResult.append([])
json.dump(batchResult, open(sys.argv[1] + '.out', 'w'))
Simple json-based heap database in Python
1 files attached: DbHeap.json
DbHeap.json
import functools
import json
import urllib.parse
import os

class Heap:
    def __init__(self, *relative_path_segments: str):
        self.relative_path_segments = relative_path_segments

    def __iter__(self):
        return iter([Heap._decode(it[0:-5]) for it in os.listdir(self._folder()) if it.endswith('.json')])

    def __setitem__(self, key, value):
        json.dump(value, open(self._key2path(key), 'w'))

    def __getitem__(self, key):
        return json.load(open(self._key2path(key), 'r'))

    @staticmethod
    def _encode(code):
        return urllib.parse.quote_plus(code, '')

    @staticmethod
    def _decode(code):
        return urllib.parse.unquote_plus(code)

    @functools.lru_cache
    def _folder(self):
        result = os.path.join(Config()['db.heap.root'], *self.relative_path_segments)
        if not os.path.exists(result):
            os.makedirs(result, True)
        if not os.path.isdir(result):
            raise Exception(f"{result} must be a folder")
        return result

    def _key2path(self, key: str):
        return os.path.join(self._folder(), self._encode(key) + '.json')
Pork-to-sausage approach to remote-download an HTTP folder using sed and wget
1 files attached: pork-to-sausage-folder-download.sh
pork-to-sausage-folder-download.sh
wget -qO- - http://host/path/ | sed -E 's/^.*href=\"(.*)\".*$/\1/g'|grep -v '/'|grep -v '>' | xargs -i wget 'http://host/path/{}'
SCP URL implementation in Java
4 files attached: SCPClient.java Handler.java URLConnection.java usage.java
SCPClient.java Handler.java URLConnection.java usage.java
public class UpDownload implements Serializable{
	static SCPClient scp= new SCPClient(host, port, user, pass);
	public void download(String remote, String local) throws IOException {
		boolean cacheInMemory = false;
		if (cacheInMemory) {
			byte[] data = scp.download(remote);
			new File(local).getParentFile().mkdirs();
			FileOutputStream fos = new FileOutputStream(local);
			fos.write(data);
			fos.close();
		} else {
			new File(local).getParentFile().mkdirs();
			FileOutputStream fos = new FileOutputStream(local);
			scp.downloadToStream(remote, fos);
			fos.close();
		}
	}
	public static void upload(String remote, String local) throws IOException {
		File file = new File(local);
		FileInputStream fis = new FileInputStream(file);
		byte[] data = new byte[(int) file.length()];
		for (int i = 0; i < data.length;)
			i += fis.read(data, i, data.length - i);
		fis.close();
		scp.upload(remote, data);
	}
	public static void main(String[] args) throws IOException {
		upload("user:pass@host/path/to/file","c:\\local\\path");
		download("user:pass@host/path/to/file","c:\\local\\path");
	}

	public static void download(String fullPath) throws IOException {
		String[] components = fullPath.split("\\/");
		String user = components[0].split("@")[0].split(":")[0];
		String pass = components[0].split("@")[0].split(":")[1];
		String host = components[0].split("@")[1];
		String fileName = components[components.length - 1];
		String folder = "";
		for (int i = 1; i < components.length - 1; i++)
			folder += "/" + components[i];
		UpDownload ud = new UpDownload(host, 22, user, pass);
		ud.download(folder + "/" + fileName, "j:/scp/" + host + "/" + folder
				+ "/" + fileName);
	}

	public static void upload(String fullPath) throws IOException {
		String[] components = fullPath.split("\\/");
		String user = components[0].split("@")[0].split(":")[0];
		String pass = components[0].split("@")[0].split(":")[1];
		String host = components[0].split("@")[1];
		String fileName = components[components.length - 1];
		String folder = "";
		for (int i = 1; i < components.length - 1; i++)
			folder += "/" + components[i];
		UpDownload ud = new UpDownload(host, 22, user, pass);
		ud.upload(folder + "/" + fileName, "j:/scp/" + host + "/" + folder
				+ "/" + fileName);
	}
}
SSH URL connection protocol implementation in Java for stateless calls
3 files attached: Handler.java URLConnection.java usage.java
Handler.java URLConnection.java usage.java
URL url = new URL(
				"ssh://user:password@host?ksh -c ls");
		URLConnection c=url.openConnection();
		c.connect();
		InputStream is=c.getInputStream();
		for(;;) {
			int i=is.read();
			if(i<0)break;
//			System.out.print((char)i);
		}
Pork-to-sausage XSL transformation of a file in Java
1 files attached: XSLT.java
XSLT.java
package dms.xml;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class XSLT{
	private Transformer transformer;
	public XSLT(InputStream xslt) throws TransformerConfigurationException{
		System.setProperty("javax.xml.transform.TransformerFactory","net.sf.saxon.TransformerFactoryImpl");
		TransformerFactory tfactory=TransformerFactory.newInstance();
//		tfactory.setAttribute("http://saxon.sf.net/feature/outputURIResolver",new UserOutputResolver());
		tfactory.setAttribute("http://saxon.sf.net/feature/allow-external-functions",Boolean.TRUE);
		tfactory.setAttribute("http://saxon.sf.net/feature/timing",Boolean.TRUE);
		transformer=tfactory.newTransformer(new StreamSource(xslt));
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	}
	public void setParameter(String name,Object value){
		transformer.setParameter(name,value);
	}
	public byte[] transform(InputStream xml) throws TransformerException{
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		transformer.transform(new StreamSource(xml),new StreamResult(baos));
		return baos.toByteArray();
	}
	public static void main(String argv[]) throws TransformerException,IOException{
		System.out.println("!!!!!!!");
		System.out.println("-----------------------");
		XSLT xslt=new XSLT(new FileInputStream("J:\\http\\applications\\WSDLConnector\\wsdl2xml.xsl"));
		byte[] result=xslt.transform(new FileInputStream("j:\\http\\applications\\WSDLConnector\\SelfCareWebServicePort.wsdl"));
//		FileOutputStream output=new FileOutputStream("J:\\DS\\qpass\\MediaMall\\storefrontpurchasewebservice.html");
//		OutputStream output=System.out;
//		output.write(result);
//		output.close();
//		System.out.println(new String(result));
		System.out.println(result.length);
	}
//	private static class UserOutputResolver implements OutputURIResolver{
//		public Result resolve(String href,String base) throws TransformerException{
//			if(href.endsWith(".out")){
//				StreamResult res=new StreamResult(System.out);
//				res.setSystemId(href);
//				return res;
//			}
//			return null;
//		}
//		public void close(Result result) throws TransformerException{}
//	}
}
Easy to use Java XSD validator
1 files attached: XSD.java
XSD.java
package dms.xml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import javax.xml.XMLConstants;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class XSD {
	public static void main(String[] args) throws SAXException,
			ParserConfigurationException, IOException {
		SchemaFactory schemaFactory = SchemaFactory
				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schemaXSD = schemaFactory
				.newSchema(new File(
						"j:\\scsjcm\\v15\\scripts\\templates\\CustomerDetailsVO.xsd"));
		Validator validator = schemaXSD.newValidator();
		validator.setErrorHandler(new ErrorHandler() {
			@Override
			public void warning(SAXParseException exception)
					throws SAXException {
				System.out.println(3);
			}

			@Override
			public void fatalError(SAXParseException exception)
					throws SAXException {
				System.out.println(1);
			}

			@Override
			public void error(SAXParseException exception) throws SAXException {
				System.out.println(exception);
			}
		});
		validator.validate(new StreamSource(new File(
				"j:\\scsjcm\\v15\\scripts\\templates\\AMSPFeed.xml")));
	}

	public static void validate(String xml, File xsd) throws SAXException,
			ParserConfigurationException, IOException {
		SchemaFactory schemaFactory = SchemaFactory
				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schemaXSD = schemaFactory.newSchema(xsd);
		Validator validator = schemaXSD.newValidator();
		validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes())));
	}
}