Thursday, August 18, 2016

Python socket server with handling multiple client connections

In this post describes how to write socket server with handling multiple client connections in python.

Creation of socket server needs following steps:
  • bind socket to port
  • start listening
  • wait for client
  • receive and send data
Sample Code for Socket Server

import socket
import sys
from thread import *
import logging

host = ''
port = 8001

logging.basicConfig(level=logging.DEBUG)

#initiate socket creating
logging.info('# creating socket')
s = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
logging.debug('# created socket : ' + str(s))

try:
    s.bind((host, port))
except socket.error as message:
    logging.error('# socket binding failed : ' + str(message))
    sys.exit()

logging.info('# socket binding success')

#start listning on socket
s.listen(5)
logging.info('# socket listning')

#handler for client threads
def client_thread_handler(conn):
    #sending msgs to connected clients
    conn.send('hi... u r now connected')

    #loop for not terminate or end thread
    while True:
        data = conn.recv(1024)
        print(data)

        if str(data) == 'exit':
            logging.info('# exiting from thread')
            break

        reply = 'reply... ' + data
        conn.sendall(reply)

    conn.close()


#waiting for data from client
while True:
    conn, addr = s.accept()
    logging.info('# connected to : ' + str(addr))
    start_new_thread(client_thread_handler, (conn,))

s.close()

After running the server will run on the localhost port 8001.
To test this you can create a socket client or telnet to port 8001.

Testing with telnet type following in shell:

telnet localhost 8001


Friday, July 29, 2016

Install Python in Ubuntu

"Python is a programming language that lets you work more quickly and integrate your systems more effectively. And it is powerful...fast...plays well with others and runs everywhere..Most of all python is open and easy to learn..... So give it a try with your back-end developments...." 

Here I'm going to discuss how to install python on Ubuntu. So here we go.......

First you have to install some dependencies before installing python..execute following commands in shell...
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

Then download python latest version from Python Site  and extract it..
tar -xvf Python-3.5.2.tar.xz
cd Python-3.5.2

Execute following commands to configure and install python...
./configure
make
sudo make install
Execute command "python" to go to python console
To exit from python console use "exit()"
use CXX=g++ ./configure if ./configure issues a warning saying g++ was not found 

Tuesday, April 28, 2015

Android SMS receive programmatically


Sending and receiving SMS messages are fundamental features on mobile devices. Android provides API for both send and receive sms to your android application.
Here is an example for catch when your android device receive SMS to it. You need to register a broadcast receiver initially for this.

For further references use following links.
SmsManager
SmsIntents