Category Archives: Python

Update Python/Install Python 3.6.0

[1] Install Packages yang di butuhkan

$ sudo apt-get install build-essential checkinstall
$ sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

[2] Download python di official site

$ cd /usr/src
$ wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz

extract file downloadpackage

sudo tar zxvf Python-3.6.0.tgz

[3] Compile Python Source

$ cd Python-3.6.0
$ sudo ./configure
$ sudo make altinstall

cek versi python

$ python3.6 -V
Python 3.6.0

Cheers – Andito Yugo Wicaksono

 

 

Advertisements

Python Scripts

1. Install Python
space@space:~$ sudo apt-get -y install python

2. Enable CGI module
space@space:~$ a2enmod cgi
Your MPM seems to be threaded. Selecting cgid instead of cgi.
Module cgid already enabled
space@space:~$ sudo systemctl restart apache2

3. Membuat python script index.cgi di default direktori /usr/lib/cgi-bin setelah melakukan enable CGI
space@space:/usr/lib/cgi-bin$ sudo vim coba
#!/usr/bin/env python
print "Content-type: text/html\n\n"
print "Hello Friend\n"

chmod 705 /usr/lib/cgi-bin/coba

#Buka tampilan web nya
selection_039

4. Jika ingin scripts CGI di manage direktori lain kecuali defaultnya, bisa di lakukan konfigurasi seperti berikut ini.
Contoh di direktori “/var/www/html/cgi-enabled”
space@space:~$ sudo vim /etc/apache2/conf-available/cgi-enabled.conf
#Untuk scripts .cgi and .py as CGI

selection_042

#Buat direktori cgi di html
space@space:~$ sudo mkdir /var/www/html/cgi-enabled
space@space:~$ sudo a2enconf cgi-enabled
#Restart apache2
space@space:~$ sudo systemctl restart apache2

5. Membuat contoh script python. Jika sudah lalu coba buka menggunakan web browser.
space@space:~$ sudo vim /var/www/html/cgi-enabled/index.py
selection_041
#Ubah permission
space@space:~$ sudo chmod 705 /var/www/html/cgi-enabled/index.py

selection_040

Sekian..cheers

Install Django

Install Django Python Web Application Framework di ubuntu 16.04

1. Install packages.
sudo apt-get install python-virtualenv

2. Install Django Virtualenv environment.
virtualenv venv
Running virtualenv with interpreter /usr/bin/python2
New python executable in /home/space/venv/bin/python2
Also creating executable in /home/space/venv/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.

space@space:~$ cd ~/venv/
space@space:~/venv$ source bin/activate
(venv) space@space:~/venv$ pip install -U pip
Requirement already up-to-date: pip in ./lib/python2.7/site-packages
(venv) space@space:~/venv$ pip install django
Collecting django
Downloading Django-1.10.2-py2.py3-none-any.whl (6.8MB)
100% |████████████████████████████████| 6.8MB 105kB/s
Installing collected packages: django
Successfully installed django-1.10.2

(venv) space@space:~/venv$ django-admin --version
1.10.2

#Keluar dari virtualenv
(venv) space@space:~/venv$ deactivate

3. Membuat contoh project.
space@space:~/venv$ cd ~/venv/
space@space:~/venv$ source bin/activate

#Buat project baru
(venv) space@space:~/venv$ django-admin startproject testproject
(venv) space@space:~/venv$ cd testproject/

#Konfigurasi database (default SQLite)
(venv) space@space:~/venv/testproject$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions

#Buat admin user
(venv) space@space:~/venv/testproject$ python manage.py createsuperuser
Username (leave blank to use 'xenial'): xenial
Email address: admin@parkit-telecommunication.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Password:
Password (again):
Superuser created successfully.

#Start Server
(venv) space@space:~/venv/testproject$ python manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
October 24, 2016 - 01:57:54
Django version 1.10.2, using settings 'testproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

4. Akses menggunakan web “http://(server’s hostname or IP address):8000/” diakses dari client.
selection_014

5. Untuk akses menggunakan user admin
selection_015

selection_016

6. Membuat test aplikasi
space@space:~/venv$ source bin/activate
(venv) space@space:~/venv$ cd testproject/
(venv) space@space:~/venv/testproject$ python manage.py startapp testapp

#Setelah selesai membuat project baru, maka lanjutkan dengan test source python code
(venv) space@space:~/venv/testproject$ sudo vim testapp/views.py
#Masukkan source berikut
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")

(venv) space@space:~/venv/testproject$ more testapp/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]

#Menghubungkan URL modul testapp ke root project yaitu di testproject, dan tambahkan impor untuk django.conf.urls.include dan masukkan include() di urlpatterns, contoh source nya:
(venv) space@space:~/venv/testproject$ more testproject/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^testapp/', include('testapp.urls')),
url(r'^admin/', admin.site.urls),
]

#Lalu jalankan
(venv) space@space:~/venv/testproject$ python manage.py runserver
Performing system checks...