Now you can Subscribe using RSS

Submit your Email

Tuesday

Open Port 443 in Azure Virtual Machine

Vishal Shrestha
In this tutorial we will see how we can open port number 443 in Azure Virtual Machine. This will allow HTTPS traffic to your website. The virtual machine can be running on anything from Linux to windows. This is pretty so simple so let's just get started.

Add 443 Endpoint in Azure Virtual Machine


  1. First of all go to Azure Portal, and login.
  2. Azure Portal
  3.  The above screenshot shows my Azure Portal. Now go to your Virtual Machine and you'll see something like this:
  4. Virtual Machine Azure

  5. Now on the left hand side you can see a lot of options, but you just need to click on the endpoints options, that's where we'll be adding the port. After clicking it, you'll see something like this:
  6. Azure Virtual Machine End Points


  7. Now you can see a list of already open ports, as you can see the https port is already opened in this picture, that is port number 443. I had already opened the port before taking the screenshot. My Bad. However you need to click on the add button on top to add any port. So just click on the add button and you'll see something like this:
  8. Azure VM adding Endpoint(Port)
  9. Now this is the last step, you can easily add the required port here and click ok at the bottom and it will be saved. Add anything as the name that will help you remember what the port is later and in the private and public port just add 443 for now. You can also have different ports in public and private port but that will need some configuration in your VM so let's just keep it the same for now. For HTTPS the protocol is TCP so keep that unchanged and also leave the floating IP disabled. Finally, hit OK and you are done
This is it, this way you can easily add port in Azure Virtual Machine, if you have any queries feel free to drop them in the comments section below.

Friday

Writing to CSV file using Python

Vishal Shrestha
CSV files are comma separated values and in literally stands for comma separated values. It is a commonly used data format and used by spreadsheets. Python has a csv module in it's standard library that has classes and methods to perform read/write operations on CSV files.

This is the basic stuff: You can change delimiter, newline, write a dictionary, write an iterable using writerows and more.

CSV File in Excel


Python Module for CSV:
There is a CSV module in python that has functions & classes available to help write and read CSVs, some of them are:
  • csv.reader function
  • csv.writer function
  • csv.DictReader class
  • csv.Dictwriter class

File Modes:
Whenever we read from or write to a file in Python, we need to open it first. After completing the required tasks, we need to be close the file so that resources that are tied with the file are freed.
  1. r for reading.
  2. r+ opens for reading and writing (cannot truncate a file)
  3. w for writing.
  4. w+ for writing and reading (can truncate a file)
  5. rb for reading a binary file. The file pointer is placed at the beginning of the file.
  6. rb+ reading or writing a binary file.
  7. wb+ writing a binary file.
  8. a+ opens for appending.

1. Create and Write to CSV:
First of all you need to create a file and you simply call a function called "open" and specify that you want to write to it. If the file doesn't exist Python will create it for you.
Here's code example:

import csv
with open('python.csv', 'w') as csvfile:
    filewriter = csv.writer(csvfile)
    filewriter.writerow(['Name', 'Address'])
    filewriter.writerow(['Arun', 'Delhi'])
    filewriter.writerow(['Vishal', 'New York'])
    filewriter.writerow(['Paul', 'Tokyo'])

2. Read CSV:
To read CSV file or any file for that matter of fact, simply use the open function. Here's an example
import csv

# create list holders for our data.
names = []
address = []

# open file
with open('python.csv', 'r') as f:
    reader = csv.reader(f)

    # read file row by row
    for row in reader:
        names.append(row[0])
        address.append(row[1])


# Print data
print names
print address




CSV File



In this tutorial we have covered most of what is required to successfully handle CSV file using the different functions and classes provided by Python. There's a lot more but CSV files have been widely used in various ares as they are easy to read and manage plus their small size makes them relatively fast to process and transfer. If you are just getting started in Python in Windows, you'll need PIP for majority of tasks, there are some more python tutorials that can help you in Video Format that you can find here:
Installing PIP in Windows

Sunday

Convert File to Byte Array in Android

Vishal Shrestha
There can be some scenarios where you will want to convert ByteArray to a File like bytearray to bitmap or audio files or any other type of file in your Android App. Simply speaking a byte array is an array of bytes, a byte consists of 8 bit. Byte array can be use dto store the collection of binary data like the contents of a file.

The following code shows how you can convert byte array to a file and a file to byte array in Android.

convert-file-to-byte-array
Convet File to Byte Array and Vice Versa

Converting File to Byte Array in Android
You can do this using input streams.
public static byte[] getByteArrayFromAudio(String filePath) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            for (int readNum; (readNum = fis.read(b)) != -1; ) {
                bos.write(b, 0, readNum);
            }
            return bos.toByteArray();
        } catch (Exception e) {
            Log.d("mylog", e.toString());
        }
        return null;
}



Converting Byte Array to File in Android
You can do this using buffered output streams.
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(yourFile));
bos.write(fileBytes);
bos.flush();
bos.close();

For example, the code below shows how you can convert a bitmap to a byte array and vice-versa.

Bitmap to Byte Array:
public static byte[] getByteArray(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
 }


Byte Array to Bitmap:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

---------------------------------------------------------------------------------------------------------------------

How to Send Emails Using Python

Vishal Shrestha
Python has some modules in the standard library that help working with emails and email servers.
In this guide we will be using the smtplib module of python. SMPTlib defines an SMTP client session object that is used to email any Internet machine with an SMTP or ESMTP listener daemon.

By the end of this guide, you'll be able to just send emails using such a simple line of code:

# Params: from_email, password, to_email, subject, email_body
mailman.sendmail('SENDER EMAIL', 'PASSWORD', 'TO EMAIL', 'SUBJECT','BODY')

In this guide, we will be sending emails using gmail's email and smtp server as example, you can use any server according to your requirement. You can checkout the video tutorial here:



There are 4 basic steps to send email with python, those are:
  1. Set up the SMTP server and log into your account.
  2. Create a MIMEMultipart object and add appropriate headers.
  3. Add message body.
  4. Send the message using the SMTP server object.
Here's the sample code:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def sendmail(from_email, password, to_email, subject, message):
    # setup the parameters of the message
    msg = MIMEMultipart()
    msg['From'] = from_email
    msg['To'] = to_email
    msg['Subject'] = subject

    # add in the message body
    msg.attach(MIMEText(message, 'plain'))
    try:

        # set up the SMTP server
        # host and port, you can leave port empty as 465 is the default anyway
        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        server.ehlo()
        server.login(msg['From'], password)
        server.sendmail(msg['From'], msg['To'], msg.as_string())
        server.close()
        return True
    except Exception as e:
        print('Something went wrong: ' + str(e))
        return False

Now you can call this method wherever you like to send emails like this:

sendmail('SENDER EMAIL', 'PASSWORD', 'TO EMAIL', 'SUBJECT',
                 'BODY')


There are a lot more things that I've not covered here, but I hope this gets you started, if you have any issues sending email from python, feel free to ask about those in the comments section below.

Saturday

Python Data Structures - Lists in Python

Vishal Shrestha
In this series we will learn about non-primitive data structures in python.
We will not go over the common primitive data structures like int, booleans, floats and string but take a deeper look into non primitive data structures that help us in organizing data and appreciate python's powerful tools a little more. In this series we will go over 4 data-structures, those are
  1. LIST
  2. DICTIONARY
  3. SET
  4. TUPLE
python lists
Lists in Python

We will learn through examples how to use them, what default methods python provides for them and which data structure to use according to our needs.

Let's go over Lists first:
Note: In case you are a video person, you can watch our full python tutorial here:




LIST

In python, Lists are similar to arrays in other languages. Lists don't always have to be homogeneous, and this it a very powerful tool for data storage in python. A single list can contain different Data types like Strings, Integers and even non primitive objects. Lists are also extremely useful for the implementation of stacks and queues. Unlike some other data structures in python that we will cover later, lists are mutable, and hence, they can be modified even after their creation.
Simply Speaking, lists are a type of container in Data Structures, which is used to store multiple data at a time. In python, lists are ordered and have a fixed count. The indexing of elements in a list are according to a definite sequence and the indexing start with 0 being the first element. Each element in the list has a fixed place in the list, this allows us to create duplicates of elements in the list, with each element having its own unique place.
Tip - Lists in python are a useful tool for preserving a sequence of data, later iterating over it and taking necessary steps. Lists can contain mutable elements unlike sets.


CREATING LISTS

Creation of Lists in Python is done by placing the sequence of data inside the square brackets[] separated by commas. Unlike Sets, we don't need a built-in function for creating list. A list can contain duplicate values with their different indexes and hence, multiple distinct or different values can be passed as a sequence we creating the list.
Let's look at the example below to understand the code better:

# Python program to demonstrate lists

# Initializing a List
List = []
print("Intialized a blank List: ")
print(List)

# Creating a List with the use of a String
List = ['python lists']
print("\nList with String: ")
print(List)

# Creating a List with multiple values
List = ["python", "list", "code"]
print("\nList with multiple values: ")
print(List[0])
print(List[1])

# Creating a Multi-Dimensional List (list inside a List)
List = [['pytho', 'list'] , ['another list']]
print("\nNested List: ")
print(List)

# Creating a List with
# the use of Numbers
# (Having duplicate values)
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("\nList with the use of Numbers: ")
print(List)

# Creating a List with multiple data types
List = [5, 4, 'python', 3, 'list', 2, 1]
print("\nList with the use of multiple datatypes: ")
print(List)


ADDING ELEMENTS IN LIST

To add elements in a list, we can use the built-in method append(). When using append() method, we can only add one element at the end of the list, to add multiple items using append(), we need to use loops. We can also add Tuples to the List with the using append method because tuples are immutable. Unlike Sets, We can also add lists to the existing list with the using the append() method.
One this to note about append() method is that it only works for the addition of elements at the end of the List, for addition of element at the any desired position, we can use the insert() method. Unlike append() which takes only one argument, we need to pass two parameters to the insert() method those are(position, value). Other than append() and insert() methods, there’s one more method for Addition of elements, extend(), this method can be used to add multiple elements at once at the end of the list.

Tip – append() & extend() methods can only add elements at the end, to add elements at desired position use the extend() method.

# Python program to demonstrate element addition in List

# Creating a List
List = []
print("Intialized blank List: ")
print(List)

# Addition of Elements
# in the List
List.append(5)
List.append(4)
List.append(3)
print("\nList after Adding three elements: ")
print(List)

# Adding elements to the List using iterator
for i in range(1, 4):
   List.append(i)
print("\nList after Addition of elements from 1-3: ")
print(List)

# Adding Tuples to the List
List.append((5, 6))
print("\nList after adding a Tuple: ")
print(List)

# Adding list to a List
List2 = ['Python', 'List']
List.append(List2)
print("\nList after adding a List: ")
print(List)

# Adding Element at a specific Position

# 12 in 4th position, python in first position
List.insert(3, 12)
List2.insert(0, 'Python')
print("\nPerforming Insert Operation: ")
print(List)

# Addition of multiple elements at the end of list
List.extend([8, 'Python', 'List])
print("\nList after Extend Operation: ")
print(List)


ACCESSING THE ELEMENTS OF A LIST

We can access the elements of a list using the index of the element in the list. The index operator [ ] is used to access an item in a list. The index of the list must be an integer. Elements in Nested lists are accessed using nested indexing.

# Creating a List with multiple values
List = ["Python", "List"]
# accessing an element from the list using index
print("Accessing a element from the list")
print(List[0])  
print(List[1])
# Creating a Multi-Dimensional List
List = [['Python', 'List'] , ['List']]
# accessing a element from the Multi-Dimensional List using
print("Acessing a element from a Multi-Dimensional list")
print(List[0][1])
print(List[1][0])
List = [1, 2, 'Python', 4, 'List', 6, 'Data']
# Accessing a element using negative indexing
print("Element using negative indexing")
# print the last element of list
print(List[-1])
# print third from the last  
print(List[-3])


REMOVING ELEMENTS FROM THE LIST

In python lists Elements are removed from the List by using built-in remove() function. If the element doesn't exist then we get an Error message. Remove() method only removes one element at a time, we can use iterator to remove range of elements. Pop() function is also used to remove & return an element from the list, but by default it removes only the last element of the set, to remove element from a specific position in the List, we need to pass index of the element as an argument to the pop() method.

Tip – Remove method only removes the first occurrence of the searched element.


# Python program to demonstrate item removal from list
# Creating a List
List = [1, 2, 3, 4, 5, 6,  
       7, 8, 9, 10, 11, 12]
print("Intialized a List: ")
print(List)
# Removing elements from List
List.remove(5)
List.remove(6)
print("\nList after Removing two elements: ")
print(List)
# Removing elements from List using an iterator
for i in range(1, 5):
    List.remove(i)
print("\nList after removal of a range of elements: ")
print(List)
# Removing element from the list using the pop() method
List.pop()
print("\nList after popping an element: ")
print(List)
# Removing element at a location from the list using the pop() method
List.pop(3)
print("\nList after popping an element at a specific: ")
print(List)

SLICING A LIST
There are many ways to print a whole List with all elements in python, but for printing a specific range of elements from the list, we use need to use the Slice operation. We can perform the slice operation on Lists with the use of colon(:). To print elements from the start to a range use [:Index], to print elements from end use [:-Index], to print elements from specific Index till the end we use [Index:], to print elements within a range, we can use [Start Index:End Index] & to print whole List with the use of slicing operation, use [:]. Further, slicing can also be used to print whole List in reverse order like this: [::-1].

Tip – To print elements of List from the end, use Negative Indexes.


# Python program to demonstrate removing elements in a List
# Creating a List
List = ['G','E','E','K','S','F',
       'O','R','G','E','E','K','S']
print("Intialized List: ")
print(List)
# Print elements in a specific range using Slice operation
Sliced_List = List[2:7]
print("\nSlicing elements in a range 2-7: ")
print(Sliced_List)
# Print elements from beginning to a predefined
Sliced_List = List[:-5]
print("\nElements sliced till 5th element from last: ")
print(Sliced_List)
# Print elements from a fixed point to end
Sliced_List = List[5:]
print("\nElements sliced from 5th "
     "element till the end: ")
print(Sliced_List)
# Printing all elements
Sliced_List = List[:]
print("\nPrinting all elements of the list: ")
print(Sliced_List)
# Printing elements in reverse
Sliced_List = List[::-1]
print("\nPrinting List in reverse order: ")
print(Sliced_List)

In this tutorial we have seen how we can create list in python, add items to them, remove items from list, access list items and finally how to slice list items. This should get you pretty familiar with the Lists in Python. If you have any questions or comments, feel free to drop them down below. Enjoy python lists, happy coding.

Tuesday

Upgrade PIP in Windows

Vishal Shrestha
This tutorial is a guide that will show you how you can upgrade PIP in Windows. The PIP is a tool that help to install and manage Python packages, such as the packages that can be found in the Python Package Index. If you are following this tutorial that you already have PIP installed in your windows machine.

Here are the topics that will be covered in this tutorial:
1. Check if PIP is installed in your Windows machine.
2. Check the version of PIP installed.
3. Upgrade the PIP version.
4. Downgrade the PIP version.

Tip: To Install PIP in windows, you can check out the following tutorial:


1. First of all to check if you have PIP installed in your windows machine, just open command line and type pip and hit enter. If you see a list of commands that look like the image below: Congrats, you have pip installed.



pip-commands
PIP Commands

2. Now to check the version of PIP installed you can enter one of the following commands:
    - pip --version or pip -V

You'll see an output like this:


pip-version
PIP Version

Here you can check your PIP version and decide whether you want to upgrade or not. No just not the version down so you can downgrade later if required.

3. Now finally, moving on to upgrade PIP on windows machine, use the following command:
    - For windows: python -m pip install --upgrade pip

You'll see the following in console and PIP will be upgraded.

upgrading-pip
Upgrading PIP in Windows

4. Now for some reason you again want to downgrade your PIP version, just pull up the PIP version that you had noted down before upgrading PIP in the previous step and enter the following command:
    - python -m pip intall pip=<your-older-pip-version>

So this is it, you don't have much complexities in upgrading PIP or downgrading PIP. I've covered most common things that you'd want to do with the PIP package. If you are having trouble using PIP you can just enter pip --help in the command line and you will be able to see a bunch of options that you can use with PIP. Happy coding, if you any trouble upgrading pip or downgrading pip, feel free to comment them below, I'll be happy to help.

Wednesday

Django Web Hosting in Apache Server

Vishal Shrestha
In this post, we'll discuss how to host a Django application in Apache Web server. Django has become my favorite web development tool in recent times, it provides a lot of tools that help develop an application in the shortest amount of time possible. However, deploying the Django application can be a bit tricky, so let's get started with how to host Django web application in an Apache server running on Ubuntu.

django-web-hosting-apache-server
Django web hosting in Apache


Prerequisites: (We'll discuss these in detail later in the tutorial)
1. Your Django Web Application
2. Apache web server
3. Django Installed
3. Python installed in your web server
4. mod_wsgi installed for Apache
5. Mysqlclient or mysql-connector-python (If database is mysql) 
Optional: Virtual Environment
Note: We will also go through the checklist for Django Web Server Hosting so that you can verify your configuration matches the required configuration.

Hosting Django Web Application in Apache Server

1. Assuming your Django application is ready, we will transfer it to the required server. For the sake of this tutorial, we will assume the root directory of the project (where manage.py is located) is root/django-app and settings.py,wsgi.py are located in root/django-app/django-app/.
If you don't have Apache installed, you can install it using: apt install apache2

2. If you don't already have python installed in you server, then go ahead and install it.
Note that your django application's python version, server's python version and the version of mod_wsgi python must be matching. In the current date, you are most probably using Django version > 1.5 and probably using python > 3.
So, for python > 3 compatible set up, install required component using the following commands:
Python: apt install python3
Django: apt install django==<your-django-project-version>
mod_wsgi: apt install libapache2-mod-wsgi-py3

If you need to set up for python 2.xx versions, then:
Python: apt install python
Django: apt install django==<your-django-project-version>
mod_wsgi: apt install libapache2-mod-wsgi

3: If you have mysql as the backend database for your application, then you need to install a mysqlclient. So install mysqlclient using:
apt install mysqlclient

If you have problems installing mysqlclient, then install mysql-connector:
apt install mysql-connector-python and then set the database engine in settings.py to  mysql.connector.django. Also change the other details to match the credentials of your live database.

4. We have already installed mod_wsgi in step 2, now we just create a virual-host for our web application and use the power of wsgi to serve requests to our Django app.
Create your website's virtual-host or edit and add the following:

        ServerName djangosite.com
        ServerAlias www.djangosite.com
        ServerAdmin webmaster@localhost

        DocumentRoot /root/django-site

        <Directory /root/django-site>
                Require all granted
        </Directory>

        <Directory /root/django-site/django-site>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        WSGIDaemonProcess daemon1 python-path=/root/django-site
        WSGIProcessGroup daemon1
        WSGIScriptAlias / /root/django-site/django-site/wsgi.py
        WSGIPassAuthorization On

Note that we are assuming all the required dependencies are installed for system wide  users and not in a virtual environment. If you have installed the required dependencies in virtual-environment, then you will need to add a python-home option with WSGIDaemonProcess like this:
WSGIDaemonProcess daemon1 python-path=/root/django-site python-home=/root/your-venv-path

We are done, restart the Apache server and your Django web application is ready to be served! and yeah a gentle reminder, set Debug = False in settings.py when you go live, don't let everybody see those nasty error reports!
I have had a lot of trouble with deploying Django apps in server in the past, the most common problem was version mismatch. So make sure your Python version, django version, mod_wsgi version are all compatible with one another. Cheers! Happy coding.

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha