Posts

PYTHON UnicodeEncodeError: 'ascii' codec can't encode character

import io,sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

Python3 + Django + uWSGI + Nginx On FreeBSD

1. uWSGI Installation. $ sudo pip-3.6 install uwsgi Collecting uwsgi Downloading uwsgi-2.0.17.tar.gz (798kB) 100% |################################| 798kB 709kB/s Installing collected packages: uwsgi Running setup.py install for uwsgi ... done Successfully installed uwsgi-2.0.17 1.1 Basic test Let’s start with a simple “Hello World” example: def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] As you can see, it is composed of a single Python function. It is called “application” as this is the default function that the uWSGI Python loader will search for (but you can obviously customize it). 1.2 Now start uWSGI to run an HTTP server/router passing requests to your WSGI application: uwsgi --http :9090 --wsgi-file foobar.py 2.Installing Django. Install Django into your virtualenv , create a new project $ mkproject django $ pip install Django Collecting Django ...

[FreeBSD] Installing and using Python's virtualenv using Python3

1. Installing Python3. $ sudo pkg install python36 Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 2 package(s) will be affected (of 0 checked): New packages to be INSTALLED: python36: 3.6.4 libffi: 3.2.1_2 Number of packages to be installed: 2 The process will require 103 MiB more space. 15 MiB to be downloaded. Proceed with this action? [y/N]: y [1/2] Fetching python36-3.6.4.txz: 100% 15 MiB 15.5MB/s 00:01 [2/2] Fetching libffi-3.2.1_2.txz: 100% 34 KiB 35.3kB/s 00:01 Checking integrity... done (0 conflicting) [1/2] Installing libffi-3.2.1_2... [1/2] Extracting libffi-3.2.1_2: 100% [2/2] Installing python36-3.6.4... Extracting python36-3.6.4: 100% Message from python36-3.6.4: =========================================================================== Note that some standard Python modules are provided as separate ports as they require additional dependencies. They are availabl...

[Python] Using Python to get weather data

Here's a simple Python code snippet for finding the weather. 1. #!/usr/bin/python3 # -*- coding: utf-8 -*- import urllib.request from contextlib import closing import xml.etree.ElementTree as ET rssurl = 'https://rss-weather.yahoo.co.jp/rss/days/4410.xml' with closing(urllib.request.urlopen(rssurl)) as res: xml = res.read() tree = ET.fromstring(xml) root = tree.findall('.') for item in root[0].iter('item'): title = item.find('title').text print(title[:title.index(' - Y')]) 2. import requests import json def get_weather(): url = 'http://weather.livedoor.com/forecast/webservice/json/v1' payload = {'city': '130010'} data = requests.get(url, params = payload).json() #resp = urllib2.urlopen('http://weather.livedoor.com/forecast/webservice/json/v1?city=%s'%citycode).read() #resp = json.loads(resp) print(data['description']['text']) ...

FreeBSD: Configuring Apache to permit CGI

In httpd.conf file make sure the "cgi" LoadModule directive has not been commented out. A correctly configured directive may look like this: <IfModule mpm_prefork_module> LoadModule cgi_module libexec/apache24/mod_cgi.so </IfModule> Explicitly using Options to permit CGI execution To allow CGI program execution for any file ending in .cgi in users' directories, you can use the following configuration. <VirtualHost *:80> DocumentRoot "/usr/local/www/apache24/e" ServerName www.example.com ErrorLog "/var/log/e-error_log" CustomLog "/var/log/e-access_log" common <Directory "/usr/local/www/apache24/e/"> Order allow,deny Allow from all Require all granted Options +ExecCGI AddHandler cgi-script .cgi </Directory> </VirtualHost> <Directory "/home/*/public_html"> Options +ExecCGI AddHandler cgi-script .cgi </Di...

Installing Apache2.4, PHP7.1 on FreeBSD

1.Install and configure Apache 2.4 Let's install Apache 2.4 first. please refer to this link . 2.Install and configure PHP 7.1 $ sudo pkg install mod_php71 Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 1 package(s) will be affected (of 0 checked): New packages to be INSTALLED: mod_php71: 7.1.12 Number of packages to be installed: 1 The process will require 4 MiB more space. 1 MiB to be downloaded. Proceed with this action? [y/N]: y [1/1] Fetching mod_php71-7.1.12.txz: 100% 1 MiB 1.2MB/s 00:01 Checking integrity... done (0 conflicting) [1/1] Installing mod_php71-7.1.12... Extracting mod_php71-7.1.12: 100% [activating module `php7' in /usr/local/etc/apache24/httpd.conf] Message from mod_php71-7.1.12: *************************************************************** Make sure index.php is part of your DirectoryIndex. You should add the following to your Apache configuration file: ...

Configuring Apache to Proxy Tomcat Using the AJP Protocol.

Step 1. Configure Tomcat Comment out the normal connector above, and uncomment the AJP connector: Adjust the port if you wish and add in the URIEncoding="UTF-8" attribute. <!-- <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> Step 2. Configure Apache Open httpd.conf and scroll to the large LoadModule section. Ensure you have the following uncommented #LoadModule proxy_module libexec/apache24/mod_proxy.so #LoadModule proxy_ajp_module libexec/apache24/mod_proxy_ajp.so In my httpd.conf, right at the bottom there is a line: Include etc/apache24/Includes/*.conf Create an ajp.conf file. You could put the configuration for AJP in the main httpd.conf file but I prefer t...

Install Tomcat8.5 on FreeBSD

Perform this command to install Tomcat8.5 $ sudo pkg install tomcat85 Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 2 package(s) will be affected (of 0 checked): New packages to be INSTALLED: tomcat85: 8.5.23 jakarta-commons-daemon: 1.0.15 Number of packages to be installed: 2 The process will require 13 MiB more space. 9 MiB to be downloaded. Proceed with this action? [y/N]: y [1/2] Fetching tomcat85-8.5.23.txz: 100% 9 MiB 9.0MB/s 00:01 [2/2] Fetching jakarta-commons-daemon-1.0.15.txz: 100% 89 KiB 91.5kB/s 00:01 Checking integrity... done (0 conflicting) [1/2] Installing jakarta-commons-daemon-1.0.15... [1/2] Extracting jakarta-commons-daemon-1.0.15: 100% [2/2] Installing tomcat85-8.5.23... Extracting tomcat85-8.5.23: 100% add tomcat85_enable=YES to /etc/rc.conf file. $ sudo sysrc tomcat85_enable=YES tomcat85_enable: -> YES start tomcat8.5 $ sudo service tomcat85 s...

How To Install JDK on FreeBSD

To install OpenJDK® 8 package use the pkg utility: $ sudo pkg install openjdk8 Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 31 package(s) will be affected (of 0 checked): New packages to be INSTALLED: openjdk8: 8.144.1 libXtst: 1.2.3 recordproto: 1.14.2 libXi: 1.7.9,1 xproto: 7.0.31 libXfixes: 5.0.3 libX11: 1.6.5,1 libxcb: 1.12_2 libXdmcp: 1.1.2 libXau: 1.0.8_3 libpthread-stubs: 0.4 kbproto: 1.0.7 fixesproto: 5.0 libXext: 1.3.3_1,1 xextproto: 7.3.0 inputproto: 2.3.2 libXrender: 0.9.10 renderproto: 0.11.1 libXt: 1.1.5,1 libSM: 1.2.2_3,1 libICE: 1.0.9_1,1 fontconfig: 2.12.1,1 freetype2: 2.8 dejavu: 2.37 mkfontscale: 1.1.2 libfontenc: 1.1.3_1 mkfontdir: 1.0.7 javavmwrapper: 2.5_2 ja...

Apache24 SSL(HTTPS) Basic Configuration and Force HTTPS connections

step 1. Edit config file '/usr/local/etc/apache24/httpd.conf' remove the # sign before these lines. #LoadModule socache_shmcb_module libexec/apache24/mod_socache_shmcb.so #LoadModule ssl_module libexec/apache24/mod_ssl.so #Include etc/apache24/extra/httpd-ssl.conf In Apache2.2 [mod_socache_shmcb] is uncommented in httpd.conf by default.From Apache 2.4 [mod_socache_shmcb] is commented step 2. edit /usr/local/etc/apache24/extra/httpd-ssl.conf file. Modify these lines and configure the appropriate settings. ServerName www.example.com:443 [optional] ServerAdmin you@example.com [optional] SSLCertificateFile "/usr/local/etc/apache24/server.crt" SSLCertificateKeyFile "/usr/local/etc/apache24/server.key" >>> ServerName domain_or_ip:443 ServerAdmin master@mail_server SSLCertificateFile "path/fullchain.pem" SSLCertificateKeyFile "path/privkey.pem" step 3. Force HTTPS connections 3 solutions. Redirect Request to SSL. Usi...

Installing Apache24 on FreeBSD.

Step 1. Perform an update on your system. freebsd-update fetch freebsd-update install Step 2. Install and configure Apache24. $ sudo pkg install apache24 Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 6 package(s) will be affected (of 0 checked): New packages to be INSTALLED: apache24: 2.4.27_1 expat: 2.2.1 pcre: 8.40_1 apr: 1.6.2.1.6.0 gdbm: 1.13_1 db5: 5.3.28_6 Number of packages to be installed: 6 The process will require 83 MiB more space. 19 MiB to be downloaded. Proceed with this action? [y/N]: y [1/6] Fetching apache24-2.4.27_1.txz: 100% 5 MiB 5.1MB/s 00:01 [2/6] Fetching expat-2.2.1.txz: 100% 111 KiB 113.2kB/s 00:01 [3/6] Fetching pcre-8.40_1.txz: 100% 1 MiB 1.2MB/s 00:01 [4/6] Fetching apr-1.6.2.1.6.0.txz: 100% 453 KiB 464.0kB/s 00:01 [5/6] Fetching gdbm-1.13_1.txz: 100% 151 KiB 155.0kB/s 00:01 [6/6] Fetching db5-5.3.28...

How do I configure Dynamic DNS Client on FreeBSD.

In First, Confirm your Domain's Nameserver has been modified. NS1.AFRAID.ORG NS2.AFRAID.ORG NS3.AFRAID.ORG NS4.AFRAID.ORG step 1. Install ddClient from ports. $ sudo pkg install ddclient Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 4 package(s) will be affected (of 0 checked): New packages to be INSTALLED: ddclient: 3.8.3_3 p5-IO-Socket-SSL: 2.051 p5-Mozilla-CA: 20160104 p5-Net-SSLeay: 1.81 Number of packages to be installed: 4 The process will require 2 MiB more space. 590 KiB to be downloaded. Proceed with this action? [y/N]: y [1/4] Fetching ddclient-3.8.3_3.txz: 100% 43 KiB 44.5kB/s 00:01 [2/4] Fetching p5-IO-Socket-SSL-2.051.txz: 100% 158 KiB 162.1kB/s 00:01 [3/4] Fetching p5-Mozilla-CA-20160104.txz: 100% 147 KiB 150.0kB/s 00:01 [4/4] Fetching p5-Net-SSLeay-1.81.txz: 100% 242 KiB 247.4kB/s 00:01 Checking integrity... done (0 conflicting) [1...

NAP,DZM,UPnP How to do Port- Forwarding (Open a Port) in your Router.

1.NAP Network address translation (NAT) is a method of remapping one IP address space into another by modifying network address information in Internet Protocol (IP) datagram packet headers while they are in transit across a traffic routing device.[1] The technique was originally used for ease of rerouting traffic in IP networks without readdressing every host. In more advanced NAT implementations featuring IP masquerading, it has become a popular and essential tool in conserving global address space allocations in face of IPv4 address exhaustion by sharing one Internet-routable IP address of a NAT gateway for an entire private network. 2.DMZ n computer security, a DMZ or demilitarized zone (sometimes referred to as a perimeter network) is a physical or logical subnetwork that contains and exposes an organization's external-facing services to an untrusted network, usually a larger network such as the Internet. The purpose of a DMZ is to add an additional layer of security to an...

What does '>/dev/null 2>&1' mean in SHELL?

'>' is for redirection '/dev/null' is a black hole where any data sent, will be discarded '2' is the file descriptor for Standard Error '&' is the symbol for file descriptor (without it, the following 1 would be considered a filename) '1' is the file descriptor for Standard Out $ cat foo.txt foo bar baz redirection is the mechanism used to send the output of a command to another place. Here, for example, we are redirecting it to a file called out.txt: $ cat foo.txt > out.txt we could rewrite the command like this: $ cat foo.txt 1> out.txt we don’t see any output in the screen. We changed the standard output (stdout) location to a file. $ cat out.txt foo bar baz if we try to cat a file that doesn’t exist, like this: $ cat nop.txt > out.txt cat: nop.txt: No such file or directory we see the error output in the screen, because we are redirecting just the standard output, not the standard error. Using std...

How to Keep Remote SSH Sessions and Processes Running After Disconnection [nohup]

We can use nohup and send a long running command to background. We can continue while the command will keep on executing in background. After that We can safely log out. With nohup command we tell the process to ignore the SIGHUP signal which is sent by ssh session on termination, thus making the command persist even after session logout. On session logout the command is detched from controlling terminal and keeps on running in background as daemon process. Here, is a simple scenario wherein, we have run find command to search for files in background on ssh session using nohup, after which the task was sent to background with prompt returning immediately giving PID and job ID of the process # nohup find / -type f > files_in_system.out 2>&1 & Resuming the session to view if job is still running When we re-login again, we can check the status of command, bring it back to foreground using 'fg jobID' to monitor its progress and so on. Below, the out...

Install curl & wget On FreeBSD

1.Run this command to Install curl. $ sudo pkg install curl Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 3 package(s) will be affected (of 0 checked): New packages to be INSTALLED: curl: 7.56.1 libnghttp2: 1.26.0 ca_root_nss: 3.32.1 Number of packages to be installed: 3 The process will require 5 MiB more space. 1 MiB to be downloaded. Proceed with this action? [y/N]: y [1/3] Fetching curl-7.56.1.txz: 100% 1 MiB 1.2MB/s 00:01 [2/3] Fetching libnghttp2-1.26.0.txz: 100% 104 KiB 106.3kB/s 00:01 Checking integrity... done (0 conflicting) [1/3] Installing libnghttp2-1.26.0... [1/3] Extracting libnghttp2-1.26.0: 100% [2/3] Installing ca_root_nss-3.32.1... Extracting ca_root_nss-3.32.1: 100% [1/3] Installing curl-7.56.1... [1/3] Extracting curl-7.56.1: 100% Message from ca_root_nss-3.32.1: ********************************* WARNING ********************************* FreeBSD doe...

FreeBSD: Install & Config Samba

1.At first update the server’s repository information. $ sudo pkg update 2.Search Samba in PKG. $ pkg search samba 3.Install the last version Samba. $ sudo pkg install samba46 Sample Output: Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 33 package(s) will be affected (of 0 checked): New packages to be INSTALLED: samba46: 4.6.8 libsunacl: 1.0 gnutls: 3.5.15 trousers: 0.3.14_1 tpm-emulator: 0.7.4_2 gmp: 6.1.2 p11-kit: 0.23.8 libtasn1: 4.12 ca_root_nss: 3.32.1 libffi: 3.2.1_1 libiconv: 1.14_11 nettle: 3.3 libidn2: 2.0.4 libunistring: 0.9.7 openldap-client: 2.4.45 python27: 2.7.14 python2: 2_3 py27-dnspython: 1.15.0 py27-setuptools: 36.2.2 tevent: 0.9.31 talloc: 2.1.9 py27-iso8601: 0.1.11 popt: 1.16_2 libinotify: 20170711_1 gamin: 0.1.10_9 glib: 2.50.2_6,1 pcre: 8.40_1 tdb: 1.3.12,1 ldb: 1.1.29_1 libarchive: 3.3.1,1 expat: 2.2.1 lzo2: 2.10_1 liblz4: 1.8.0,1 Number of...

FreeBSD: Install PostgreSQL Server & Client

1.At first update the server’s repository information. $ sudo pkg update 2.Search PostgreSQL in PKG. $ pkg search postgresql 3.Install the last version PostgreSQL. $ sudo pkg install postgresql10-server Sample Output: Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 5 package(s) will be affected (of 0 checked): New packages to be INSTALLED: postgresql10-server: 10.1 libxml2: 2.9.4 postgresql10-client: 10.1 perl5: 5.24.3 readline: 7.0.3 Number of packages to be installed: 5 The process will require 88 MiB more space. 21 MiB to be downloaded. Proceed with this action? [y/N]: y [1/5] Fetching postgresql10-server-10.1.txz: 100% 4 MiB 4.1MB/s 00:01 [2/5] Fetching libxml2-2.9.4.txz: 100% 802 KiB 820.8kB/s 00:01 [3/5] Fetching postgresql10-client-10.1.txz: 100% 2 MiB 2.6MB/s 00:01 [4/5] Fetching perl5-5.24.3.txz: 100% 13 MiB 14.0MB/s 00:01 [5/5] Fetching readlin...

FreeBSD: Update/Upgrade

config file: /etc/freebsd-update.conf 1.Run this command to update FreeBSD. # freebsd-update fetch # freebsd-update install daily update job: /etc/crobntab @daily root freebsd-update cron 2.Run this command to Uninstalling updates. # freebsd-update rollback 3.Run this command to Upgrade. # freebsd-update -r xx.x-RELEASE upgrade # freebsd-update install

FreeBSD: Install sudo Command, Allow wheel Group Users To Execute A Command As The Root [Without a Password]

1.Update the pkg tool # pkg update 2.Install sudo # pkg install sudo 3.Run 'visudo' command to configure sudo. # visudo 4.Uncomment to Allow members of group wheel to execute any command. # %wheel ALL=(ALL) ALL 5.Uncomment to Allow members of group wheel to execute any command without a password. # %wheel ALL=(ALL) NOPASSWD: ALL 6.Save and close the file.