Skip to content

Instantly share code, notes, and snippets.

@zhangzhhz
zhangzhhz / python_load_script_in_shell.md
Last active June 4, 2020 06:23
Python: load a script in Python shell

Python: load a script in Python shell

This is usually for some quick testing.

  1. python -i myscript.py

    Then you can reference functions and variables in the script directly.

  2. While you are already in a Python shell:

pip was broken after I installed pip3.8 and removed some other python links.

To fix pip:

python2 -m pip install --user --upgrade pip

python3 -m pip install --user --upgrade pip

@zhangzhhz
zhangzhhz / python_proxy.txt
Created April 8, 2020 08:12
python program or PIP accessing internet via proxy
Set environment variables for proxies.
Windows:
set http_proxy=http://xxx.xxx.xxx.xxx:80
set https_proxy=http://xxx.xxx.xxx.xxx:80
Bash:
http_proxy=http://xxx.xxx.xxx.xxx:80
https_proxy=http://xxx.xxx.xxx.xxx:80
@zhangzhhz
zhangzhhz / common_MariaDB_commands.md
Last active November 29, 2021 04:37
Common Useful MySQL / MariaDB Commands
@zhangzhhz
zhangzhhz / WordPress_v5.3.2_with_Nginx.md
Last active June 23, 2020 17:24
Install WordPress v5.3.2 and self-host it over https using Nginx.
Perl:
print "$^V\n"; # 5.26.1
print "$]\n"; # 5.026001
Python:
import sys
print(sys.version) # 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]
print(sys.version_info) # sys.version_info(major=3, minor=7, micro=0, releaselevel='final', serial=0)
import platform
print(platform.python_version()) # 3.7.0
const fs = require('fs');
function packFile(filename, outfilename, noOfBytesToPrefix = 100) {
const filenameEncoded = new Buffer.from(filename, 'utf8');
const filenameLength = filenameEncoded.length;
if (filenameLength > noOfBytesToPrefix) {
throw new Error("file name after utf-8 encoding is too long");
}
let prefixBuffer = new Buffer.allocUnsafe(noOfBytesToPrefix).fill(0);
const crypto = require('crypto');
const util = require('util');
const randomBytes = util.promisify(crypto.randomBytes);
async function hashPassword(password) {
// Hash a password for storing.
try {
const buf = await randomBytes(64);
const salt = crypto.createHash('sha256').update(buf.toString('hex')).digest('hex');
import hashlib, binascii, os
def hash_password(password):
"""Hash a password for storing."""
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
pwdhash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'),
salt, 100000)
pwdhash = binascii.hexlify(pwdhash)
return (salt + pwdhash).decode('ascii')
def pack_file(file_name, outfile_name, no_of_bytes_to_prefix=100):
file_name_encoded = bytes(file_name, encoding='utf-8')
file_name_length = len(file_name_encoded)
if file_name_length > no_of_bytes_to_prefix:
raise Exception('file name after utf-8 encoding is too long')
prefixed_bytes = bytearray(no_of_bytes_to_prefix)
prefixed_bytes[0:file_name_length] = file_name_encoded
chunk_buffer = 4096
with open(file_name, 'rb') as infile, open(outfile_name, 'wb') as outfile: