Friday, April 6, 2018

Simple Server and Client Chat using Python

Server-Client Chat Python

In Python language, socket (or network socket) is a module used to communicate between two computers. It provides two types of interface to access the network, namely low-level (platform dependent connections — Example: Telnet) and high-level (application dependent connections — Example: HTTP, FTP, SMTP, etc.). This is a simple tutorial to establish the low-level socket connection between server and client to communicate messages using the TCP/IP protocol.

Server and Client Chat

In this tutorial, I have used two scripts server.py to serve data by sending an invitation to the client, and client.py to receive data on acceptance of the invitation. After accepting the invitation, both server and client share messages mutually.

— Server —

An server script performs the sequence of functions such as socket(), bind(), listen(), and accept() (repeats for more than one client) to communicate with the client. The description of each functions used in the server script are given bellow:

  • socket() – creates a socket using the address family, socket type and protocol
  • bind() – binds the socket to the given address (host name, and port number pair)
  • listen() – enables a server to accept connections from the client(s)
  • accept() – waits and accepts connection request from the client(s)
  • gethostname() – retrieves host name of the machine
  • gethostbyname() – translates a host name to IPv4 format address
  • recv() – receives message sent through TCP
  • decode() – decodes the message using the codec
  • send() – sends message sent through TCP
For more details on methods, functions, module, and objects, refer to the official site of Python (python.org).

Source Code

# server.py
import time, socket, sys

print("\nWelcome to Chat Room\n")
print("Initialising....\n")
time.sleep(1)

s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")\n")
name = input(str("Enter your name: "))
           
s.listen(1)
print("\nWaiting for incoming connections...\n")
conn, addr = s.accept()
print("Received connection from ", addr[0], "(", addr[1], ")\n")

s_name = conn.recv(1024)
s_name = s_name.decode()
print(s_name, "has connected to the chat room\nEnter [e] to exit chat room\n")
conn.send(name.encode())

while True:
    message = input(str("Me : "))
    if message == "[e]":
        message = "Left chat room!"
        conn.send(message.encode())
        print("\n")
        break
    conn.send(message.encode())
    message = conn.recv(1024)
    message = message.decode()
    print(s_name, ":", message)

— Client —

An client script performs the sequence of functions such as socket(), and connect() to communicate with the server. The description of each functions used in the server script are given bellow:

  • socket() – creates a socket using the address family, socket type and protocol
  • connect() – connects to a server socket at address
For more details on methods, functions, module, and objects, refer to the official site of Python (python.org).

Source Code

# client.py
import time, socket, sys

print("\nWelcome to Chat Room\n")
print("Initialising....\n")
time.sleep(1)

s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")\n")
name = input(str("Enter your name: "))
           
s.listen(1)
print("\nWaiting for incoming connections...\n")
conn, addr = s.accept()
print("Received connection from ", addr[0], "(", addr[1], ")\n")

s_name = conn.recv(1024)
s_name = s_name.decode()
print(s_name, "has connected to the chat room\nEnter [e] to exit chat room\n")
conn.send(name.encode())

while True:
    message = input(str("Me : "))
    if message == "[e]":
        message = "Left chat room!"
        conn.send(message.encode())
        print("\n")
        break
    conn.send(message.encode())
    message = conn.recv(1024)
    message = message.decode()
    print(s_name, ":", message)

Implementation Steps

1. Run the server.py script in Python application.

Python Open With

2. Note down the local IP address and pass it to the clients (invitation). Alternatively, you can also get the local IP address through online using L-IP tool.

Python Server Chat

3. Run the client.py script in Python application using the local IP address sent by the server (accept the invitation).

Python Client Chat

4. Sent and/or receive messages from the server/client mutually.

Python Server Chat Messages

Wednesday, March 28, 2018

Retrieving Windows Product Key using VB Script

Output of VB Script

In this article, I present the simple and safe method to retrieve product key of Windows operating system using VB Script. The VB Script reads the value of Windows product from the Registry Editor (regedit) and translates it to a formatted product key (25 alphanumeric characters). Also, it creates the backup of the product information to the local drive (Desktop).

Instructions

Step 1: Create a VB Script file “WinProductKey.vbs” using any ASCII text editor and enter the following codes.

Source Code

Dim objshell, path, DigitalID
Set objshell = CreateObject("WScript.Shell")
'Set registry key path
Path = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
'Registry key value
DigitalID = objshell.RegRead(Path & "DigitalProductId")
Dim ProductName, ProductID, ProductKey, ProductData
'Get ProductName, ProductID, ProductKey
ProductName = "Product Name: " & objshell.RegRead(Path & "ProductName")
ProductID = "Product ID: " & objshell.RegRead(Path & "ProductID")
ProductKey = "Product Key: " & ConvertToKey(DigitalID) 
ProductData = ProductName  & vbNewLine & ProductID  & vbNewLine & ProductKey
'Show messbox if save to a file 
If vbYes = MsgBox(ProductData & vblf & vblf & "Do you want to save it to Desktop?", vbYesNo, "Microsoft OS Product Key Information") then
   Save ProductData 
End If

'Convert Binary to Characters
Function ConvertToKey(Key)
    Const KeyOffset = 52
    Dim Maps, i, j, Current, KeyOutput, Last, keypart1, insert
    i = 24
    Maps = "BCDFGHJKMPQRTVWXY2346789"
    Do
        Current = 0
        j = 14
        Do
           Current = Current* 256
           Current = Key(j + KeyOffset) + Current
           Key(j + KeyOffset) = (Current \ 24)
           Current = Current Mod 24
            j = j -1
        Loop While j >= 0
        i = i -1
        KeyOutput = Mid(Maps, Current+1, 1) & KeyOutput
        Last = Current
    Loop While i >= 0 
    keypart1 = Mid(KeyOutput, 2, Last)
    insert = "N"
    KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0)
    If Last = 0 Then KeyOutput = insert & KeyOutput
    ConvertToKey = Mid(KeyOutput, 1, 5) & "-" & Mid(KeyOutput, 6, 5) & "-" & Mid(KeyOutput, 11, 5) & "-" & Mid(KeyOutput, 16, 5) & "-" & Mid(KeyOutput, 21, 5)
End Function

'Save the data to a file
Function Save(Data)
    Dim fso, fName, txt, objshell, UserName
    Set objshell = CreateObject("wscript.shell")
    'Get current user name 
    UserName = objshell.ExpandEnvironmentStrings("%UserName%") 
    'Create a text file on desktop 
    fName = "C:\Users\" & UserName & "\Desktop\WinKeyInfo.txt"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set txt = fso.CreateTextFile(fName)
    txt.Writeline Data
    txt.Close
End Function

Step 2: Double-click the “WinProductKey.vbs” file to run the VB Script (image shown at top).

Step 3: Click “Yes” button present near the prompt (“Do you want to save it to Desktop?”), to backup the information to the Desktop.

Windows Product Key Backup

Sunday, March 18, 2018

Installing Latest Apache Web Server on Windows 10

Apache Notification Icon

This tutorial describes step-by-step instructions to install the latest Apache HTTP Server software (latest stable version is 2.4.32, as on date 18 March 2018) on Windows 10 operating system.

About Apache HTTP Server

Apache HTTP Server (“Apache” and “httpd”) was launched in 1995 and it has been the most popular web server on the Internet since April 1996. The Apache HTTP Server Project is a collaborative software development effort aimed at creating a robust, commercial-grade, featured, and freely-available source code implementation of an HTTP (Web) server. This project is part of the Apache Software Foundation.

The latest released version of Apache HTTP Server Project is Apache HTTP Server 2.4.29 (as on date 18 March 2018). This version of Apache is the latest GA release of the new generation 2.4.x branch of Apache HTTPD and represents fifteen years of innovation by the project, and is recommended over all previous releases.

Downloading Apache HTTP Server

The Apache HTTP Server Project itself does not provide binary releases of software, only source code. Individual contributors may provide binary packages as a convenience, but it is not a release deliverable. If you cannot able to compile the Apache HTTP Server yourself, you can obtain a binary package from numerous binary distributions available on the Internet. The final and official binary release of Apache HTTP Server software is Apache HTTP Server 2.2.25, which can be download from https://archive.apache.org/dist/httpd/binaries/win32/.

There are popular options suggested by Apache for deploying Apache HTTPD, and, optionally, PHP and MySQL, on Microsoft Windows, include:

For more details on Apache installation in windows OS, refer to the official site of Apache (apache.org).

Step-by-Step Instruction

1. Open downloads page of Apache Haus and click the image link near ‘Download Locations’ to download Apache 2.4.32 x64 (“httpd-2.4.32-o102n-x64-vc14.zip”) binary for the 64-Bit operating system.
Apache Haus Downloads
2. Decompress the downloaded file (“httpd-2.4.32-o102n-x64-vc14.zip”) and copy the folder “Apache24” to the “C:” drive.
Apache Zip File
The “Apache24” directory consists of major sub-directories such as “bin” (binary files), “htdocs” (web pages), “conf” (configuration files), “cgi-bin” (CGI server scripts), “error” (web template files for displaying server errors), “icons” (Apache server web page theme images), and other sub-directories/help files.
Apache Files and Folders
3. Open the “bin” sub-directory and run the “ApacheMonitor” binary with administrator privileges.
Apache Bin Folder
4. If you have done step 3 successfully, the “ApacheMonitor” icon appears in the notification area (bottom-right corner of the taskbar). Right-click the icon and select “Open Apache Monitor” from the menu.
Apache Monitor Icon in Notification
5. Select “Apache2.4” from the “Service Status:” and click “Start” button to run the Apache HTTP Server. If the process is successful, a small green icon appears near the list “Apache2.4” in the “Service Status:”. Click “OK” button to hide the window.
Apache Service Monitor
6. Open the web browser and enter the IP “http://127.0.0.1“ or URL “http://localhost” in the address bar to open the test page of Apache HTTP Server. You will be greeted by the following page.
Apache Test Page
We can also test SSL (HTTPS) by entering the IP to “https://127.0.0.1” or URL “https://localhost” in the address bar to open the test page of Apache HTTP Server with SSL enabled.

Sunday, October 1, 2017

Simple WebServer using Python

It Works!

This is a simple example to develop your own Python web server used for application development or testing Python script and should not be used on a public network. It works similar to the Apache web server with limited functionalities and not intended to be a full-featured web server. This tutorial demonstrates just to activate the built-in web server through a batch file.

Instructions

For developing a Python web server, we simply need Python software alone. The batch file (.bat) was created to activate the built-in web server, automatically open the default web browser with custom folder as root directory (www), and minimize the command line window while running the Python script.

Step 1: Download and install Python software according to your system platform. I have downloaded Windows x86-64 executable installer for 64-bit Windows 10 operating system.

Python Command Line

Step 2: Create a new folder named www (optional, your path to the webpages). Also, create a new file index.html in the www folder with the following line “<h1>It Works!</h1>” and save it.

Step 3: Create an batch file PythonServer.bat in the www folder using any ASCII text editor and enter the following commands.

START /min python -S localhost 80 START "" http://localhost

Step 4: Double-click PythonServer.bat file to run web server and launch the web browser.

It Works!

Simple WebServer using PHP

It Works!

This is a simple example to design your own PHP web server used for application development or testing PHP script and should not be used on a public network. It works similar to the Apache web server with limited functionalities and not intended to be a full-featured web server. This tutorial demonstrates just to activate the built-in web server through a batch file.

Instructions

For designing a PHP web server, we simply need PHP software alone. The batch file(.bat) was created to activate the built-in web server, automatically open the default web browser with custom folder as root directory (www), and minimize the command line window while running the PHP script.

Step 1: Download and extract compresed file (.zip) formatted PHP software according to your system platform. I have downloaded VC14 x64 Thread Safe (2017-Sep-26 23:04:10) for 64-bit Windows 10 OS.

PHP 7.1

Step 2: Create a new folder www in the extracted folder. Also, create a new file index.html in the www folder with the following line “<h1>It Works!</h1>” and save it.

Step 3: Create an batch file PHPServer.bat in the extracted folder using any ASCII text editor and enter the following commands.

START /min php -S localhost:88 -t www/ START "" http://localhost:88

Step 4: Double-click PHPServer.bat file to run web server and launch the web browser.

It Works!

Frequency Plot of Protein Sequence using PHP and R

Frequenc Plot

About Protein Frequency Plot

A frequency plot is a graphical data analysis technique for summarizing the distributional information of a variable. The response variable is divided into equal sized intervals (or bins). The number of occurrences of the response variable is calculated for each bin. In this tutorial, the number of occurrences of each amino acids in the protein sequence (response variable) is calculated and sorted in ascending order.

The frequency plot then consists of:

Vertical Axis = Amino acids
Horizontal Axis = Frequencies of the amino acids

There are 4 types of frequency plots:

  1. Frequency plot (absolute counts);
  2. Relative frequency plot (convert counts to proportions);
  3. Cumulative frequency plot;
  4. Cumulative relative frequency plot.

The frequency plot and the histogram have the same information except the frequency plot has lines connecting the frequency values, whereas the histogram has bars at the frequency values.

Frequency plot using PHP and R

In this tutorial, the programming language R, PHP, and BioConductor packages SeqinR & Biostrings are used to generate a frequency plot from the protein sequence. SeqinR is used to read or manipulate sequences, and Biostrings is used to convert sequence to array. The PHP language is used to execute Rscript at background using exec() function of PHP and the image generated through R (Rscript) is retrieved and displayed through the IMG HTML tag. The execution process acts similar to PHP/CGI or Perl/CGI. For generating a frequency plot, we need a protein sequence in .fasta|.fas file format as input. A simple protocol for generating a frequency plot is given below:

Step 1: Download and install R software according to your system platform.

Step 2: Download SeqinR and Biostrings module from CRAN and install. The brief explanations for Step (1) & (2) can be downloaded from http://www.biogem.org/downloads/notes/Installing%20R.pdf

Step 3: Create an R script as given bellow using an ASCII editor (Eg. Notepad) and save it with .R file extension. Here args function is used to get path of the FASTA file formatted protein sequence through command line.

R Source Code (Freq.R)

args <- commandArgs(TRUE) fas_file <- args[1] library("seqinr") library("Biostrings") seqfile <- read.fasta(file = fas_file) fastaseq <- seqfile[[1]] seqstring <- c2s(fastaseq) seqstring <- toupper(seqstring) seqchar <- s2c(seqstring) tab <- table(seqchar) taborder <- tab[order(tab)] names(taborder) <- aaa(names(taborder)) png(filename="freq.png", width=500, height=500) dotchart(taborder, pch=19, main="Frequency of Amino Acids", xlab="Frequency", ylab="Amino Acid") dev.off()

Step 4: Create an PHP program to get protein sequence and execute the Rscript in commandline using exec() function. Example, exec("\"C:\Program Files\R\R-3.3.1\bin\Rscript.exe\" Freq.R $input");

FreqPlot PHP/R Input

PHP Source Code (Freq.PHP)

<!DOCTYPE html"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Frequency Plot using PHP and R</title> <style type="text/css"> html { box-sizing: border-box; -webkit-text-size-adjust: 100%; -webkit-font-smoothing: antialiased; } html, body { height: 100%; margin: 0px; padding: 0px; } .form{ position: absolute; top: 50%; left: 50%; width: 510px; height: 320px; margin-top: -160px; /* Half the height */ margin-left: -255px; /* Half the width */ vertical-align: middle; border: 1px solid blue; box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset; border-radius: 2px; font-family: "Times New Roman", Georgia, Serif; } .output{ position: absolute; top: 50%; left: 50%; width: 512px; height: 584px; margin-top: -292px; /* Half the height */ margin-left: -256px; /* Half the width */ vertical-align: middle; border: 1px solid blue; font-size: 12px; box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset; border-radius: 2px; } .space { padding: 10px; } .effect { border: 1px solid #aaa; box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset; border-radius: 2px; } .heffect { border: 0; height: 1px; background: blue; } a { text-decoration: none; } </style> </head> <body> <?php if(isset($_POST['submit'])) { function img2data($image) { $type = pathinfo($image, PATHINFO_EXTENSION); $data = file_get_contents($image); return 'data:image/' . $type . ';base64,' . base64_encode($data); } $input = pathinfo($_FILES['seqfile']['tmp_name'], PATHINFO_FILENAME) . ".fasta"; move_uploaded_file($_FILES['seqfile']['tmp_name'], $input); exec("\"C:\Program Files\R\R-3.3.1\bin\Rscript.exe\" Freq.R $input"); $s = img2data("freq.png"); unlink("freq.png"); unlink($input); ?> <div class='output'> <h2 style="text-align: center;">Frequency Plot using PHP and R</h2> <hr class='heffect' /> <div class='space'><?php print "<img src='" . $s . "' />"; ?></div> </div> <?php } else { ?> <div class="form"> <h3 style="text-align: center;">Frequency Plot using PHP and R</h3> <hr class="heffect" /> <form action="" method="post" name="freqform" enctype= "multipart/form-data"> <p style="padding: 0 10px 0 10px;"> Upload protein sequence (<i>fasta file</i>) <input class="effect" type="file" name="seqfile" /> </p> <p style="padding: 0 10px 0 10px;"> <input class="effect" type="reset" name="reset" value="Reset" /> <input class="effect" type="submit" name="submit" value="Generate Plot" /> </p> </form> </div> <?php } ?> </body> </html>

Program Output

FreqPlot PHP/R Output