NodeJs HTTPS.Get Example with Error Handling

Here is an example of using NodeJs “https” library with error handling: var dateformat = require(‘dateformat’); var https = require(‘https’);   try { var httpsget = https.get(‘’, function(res) { console.log(‘[HTTPS.Get] %s StatusCode:’, dateformat(new Date(), "yyyy-mm-dd h:MM:ss"), res.statusCode); }).on("error", function(error) { console.log(‘[HTTPS.Get] %s Error:’, dateformat(new Date(), "yyyy-mm-dd h:MM:ss"), error.message); }); } catch(e) { console.log(‘[HTTPS.Get] %s Error:’, […]

How to Find PHP.ini Location

Need to know where is located your PHP.ini file? Locate PHP.ini with Find The best way (in my opinion) is to use “find” to locate the php.ini: find / -name php.inifind / -name php.ini You should get something like this: /etc/php/7.3/fpm/php.ini /etc/php/7.3/cli/php.ini/etc/php/7.3/fpm/php.ini /etc/php/7.3/cli/php.ini So this way you will locate PHP.ini for both PHP7 FPM and […]

How to Enable Display of Errors in PHP

First edit your PHP.ini file to make sure display_errors is enabled: display_errors = ondisplay_errors = on Then if you changed this value, you’ll need to restart PHP7-FPM process: /etc/init.d/php7.3-fpm restart/etc/init.d/php7.3-fpm restart Additionally you may prepend these lines on your PHP script: error_reporting(E_ALL); ini_set(‘display_errors’, 1); ini_set(‘display_startup_errors’, 1);error_reporting(E_ALL); ini_set(‘display_errors’, 1); ini_set(‘display_startup_errors’, 1);

Go One Directory Level Up/Back with Dirname() in PHP

For PHP < 5.3 use this: echo realpath(dirname(__FILE__) . '/..');echo realpath(dirname(__FILE__) . '/..'); For PHP 5.3 to 5.6 use this: echo realpath(__DIR__ . '/..');echo realpath(__DIR__ . '/..'); For PHP >= 7.0 use this: echo dirname(__DIR__, 1);echo dirname(__DIR__, 1); References:

Save a String to File in Python

A simple way to save a string (or whatever) to a file in Python is: file = open(‘File.txt’, ‘w’) file.write(‘whatever’) file.close()file = open(‘File.txt’, ‘w’) file.write(‘whatever’) file.close() An alternative method that auto-closed the file is: with open(‘File.txt’, ‘w’) as file: file.write(‘whatever’)with open(‘File.txt’, ‘w’) as file: file.write(‘whatever’)

Create a File Name with Current Date & Time in Python

In Python you can get the current date and time like this: import time timestr = time.strftime("%Y-%m-%d_%H-%M-%S") print(timestr)import time timestr = time.strftime("%Y-%m-%d_%H-%M-%S") print(timestr) Here is an example output: 2019-08-11_15-23-022019-08-11_15-23-02 Then you can use timestr variable as file name, i.e: file = open(timestr+’.txt’, ‘w’) file.write(‘whatever’) file.close()file = open(timestr+’.txt’, ‘w’) file.write(‘whatever’) file.close() Or another example below: with […]

Get Current Script Path in Python

Snippet to get current script directory in Python: import os print(os.path.dirname(os.path.realpath(__file__)))import os print(os.path.dirname(os.path.realpath(__file__))) It will output the current Python script path, i.e: /path/to/python/script/path/to/python/script

Python Wildcard Search a String or Array

In Python you can use fnmatch to enable wildcard search: import fnmatchimport fnmatch Wildcard search and array of strings: lst = [‘string1’, ‘testing’, ‘football’]   if fnmatch.filter(lst, ‘th?s’): print(‘Matched’)lst = [‘string1’, ‘testing’, ‘football’] if fnmatch.filter(lst, ‘th?s’): print(‘Matched’) Wildcard search a string: if fnmatch.fnmatch(‘readme.txt’, ‘*.txt’): print(‘Matched’)if fnmatch.fnmatch(‘readme.txt’, ‘*.txt’): print(‘Matched’)

Get Current Script Path in PHP

Snippet to get current script path in PHP: echo realpath(dirname(__FILE__));echo realpath(dirname(__FILE__)); It will output the current PHP script path, i.e: /var/www/

E: Could not get lock /var/lib/dpkg/lock

If you get the following error while doing apt-get upgrade: E: Could not get lock /var/lib/dpkg/lock – open (11: Resource temporarily unavailable) E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?E: Could not get lock /var/lib/dpkg/lock – open (11: Resource temporarily unavailable) E: Unable to lock the administration directory (/var/lib/dpkg/), is […]