Friday, September 27, 2019

Set TimeZone Ubuntu 18

Set TimeZone Ubuntu

$ timedatectl list-timezones
$ sudo timedatectl set-timezone Asia/Bankok

Thursday, September 26, 2019

Tmux Tips

Tmux Tips

Tmux copy to ClipBoard

- ctrl + b, [ :Enter To Copy Mode
- Move Start/End with Arrow KEY
- ctrl + space :Start select hi-light
- alt + w :Copy To ClipBoard
- ctrl +b, ] :Paste

Tmux Create New windows

- ctrl + b, c :Create New Windows Terminal
- ctrl + b, n :Next Windows
- ctrl + b, 0-n :Select Windows by Number
- ctrl + b, w :List Windows Terminal

Tmux split panes

- ctrl + b, % :Split Vertical
- ctrl + b, " :Split Horizontal

SEND HTTP POST request using UTL_HTTP in Oracle Database 11G


grant execute on utl_http to wc
grant execute on dbms_lock to wc;

BEGIN
  DBMS_NETWORK_ACL_ADMIN.create_acl (
    acl          => 'local_test_acl_file.xml', 
    description  => 'A test of the ACL functionality',
    principal    => 'WC',
    is_grant     => TRUE, 
    privilege    => 'connect',
    start_date   => SYSTIMESTAMP,
    end_date     => NULL);
end;
/

begin
  DBMS_NETWORK_ACL_ADMIN.assign_acl (
    acl         => 'local_test_acl_file.xml',
    host        => 'ws.domain.local', 
    lower_port  => 5000,
    upper_port  => NULL);    
end;
/


create or replace
procedure send_post_requests
( p_string in varchar2
, p_number in number
) is
  req utl_http.req;
  res utl_http.resp;
  url varchar2(4000) := 'http://ws.domain.local:5000/test';
  name varchar2(4000);
  buffer varchar2(4000); 
  content varchar2(4000) := '{
"string_type":"'||p_string||'",
"number_type":"'||p_number||'"
}';

begin
  req := utl_http.begin_request(url, 'POST',' HTTP/1.1');
  utl_http.set_header(req, 'user-agent', 'mozilla/4.0'); 
  utl_http.set_header(req, 'content-type', 'application/json'); 
  utl_http.set_header(req, 'Content-Length', length(content));

  utl_http.write_text(req, content);
  res := utl_http.get_response(req);
  -- process the response from the HTTP call
  begin
    loop
      utl_http.read_line(res, buffer);
      dbms_output.put_line(buffer);
    end loop;
    utl_http.end_response(res);
  exception
    when utl_http.end_of_body 
    then
      utl_http.end_response(res);
  end;
end send_post_requests;
/

begin
   send_post_requests('stringggggggg', 123456789);
end;
/

drop procedure send_post_requests;

begin
   DBMS_NETWORK_ACL_ADMIN.UNASSIGN_ACL(
     host        => 'tko.nida.local',
     lower_port  => 5000);
end;


begin
   DBMS_NETWORK_ACL_ADMIN.DROP_ACL(
      acl => 'local_test_acl_file.xml');
end;

/








Tuesday, September 10, 2019

Example Python Decorator

ex.1
====

from functools import wraps

from flask import request, jsonify

def token_admin_required(f):
    @wraps(f)
    def decorated(*agrs, **kwagrs):

        token = None

        if 'X-API-KEY' in request.headers:
            token = request.headers['X-API-KEY']

        if not token:
            return jsonify({'message': 'Token is missing.'})

        if token != 'token-key':
            return jsonify({'message': 'Your Token is Wrong or Invalid'})

       # check token
        # print('TOKEN: {}'.format(token) )
        return f(*agrs, **kwagrs)

    return decorated

ex.2
====

import time
import datetime


def decorators(f):
    def wrappers(*args, **kwargs):
        print('Before')
        ret = f(*args, **kwargs)
        print('After')
        return ret
    return wrappers


def timer(f):
    def wrappers(*args, **kwargs):
        before = time.time()
        ret = f(*args, **kwargs)
        print("Function took: ", time.time() - before, "second")
        return ret
    return wrappers


def logger(func):
    def wrappers(*args, **kwargs):
        with open('log.txt', 'a') as f:
            f.write("called function with " + " ".join(
                [str(arg) for arg in args]) + " at " + str(datetime.datetime.now()) + "\n")
            ret = func(*args, **kwargs)
            return ret
    return wrappers


@logger
def add(a, b):
    return a + b


print(add(3, 4))




Monday, September 9, 2019

MongoDB --auth Mode with Docker

#create volume mongo-auth
docker create volume --name mongo-auth

​#Start Container id with option --auth:
​docker run -d --name mongo_auth -p 27017:27017 -v mongo-auth:/data/db mongo --auth
docker exec -it container_id mongo admin

#Add administrator user for "admin" database
db.createUser({ user: 'admin', pwd: 'password', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });

#Logout and Login Again with user/password
docker exec -it container_id mongo -u username databasename
ex.

docker exec -it container_id mongo -u admin admin

#Create News Database

use NEWDB

#Add AD admin user for "NEWDB" database
db.createUser({ user: 'app', pwd: 'password', roles: ["readWrite", "dbAdmin"] });

#Login New Mongo DB with user Authentication
​docker exec -it Container_ID mongo -u database_username  DatabaseName​
ex.

​docker exec -it Container_ID mongo -u app NEWDB​

ALCATEL 6900

write memory copy running certified reload from working no rollback-timeout