Wednesday 14 October 2015

Socket prog in Python

SERVER.py

import sys
import socket
if len(sys.argv) != 2:
    print "Server"
    sys.exit()
port = int(sys.argv[1])
sd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#returns an object
#AF.INET constants represent the address (and protocol) families used for the first argument to socket().
#SOCK_STREAM  These constants represent the socket types
#sd is the object of socket here
address = ("0.0.0.0", port)# address is just a variable
#Bind the socket to address. The socket must not already be bound
sd.bind(address)
print 'Successfully started the server.'

sd.listen(2)
print "Listening for connections now."
sock, addr = sd.accept()

file_name_len = int(sock.recv(2))
file_name = sock.recv(file_name_len)
print "Receiving " + file_name + " from " + addr[0]
outfile = open(file_name, 'w')
# Read 4096 bytes at a time and write them to
# the file. If an empty string is read, it means
# the end of the file has been reached.
while True:
    data = sock.recv(4096)
    if data != '':
        outfile.write(data)
    else:
        break
outfile.close()
print "Done."



CLIENT.py

import sys
import socket
if len(sys.argv) != 4:
    print "Client"
    sys.exit(0)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = (sys.argv[1], int(sys.argv[2]))
file_name = sys.argv[3]
sock.connect(address)
file_p = open(file_name, 'r')
file_data = file_p.read()
file_name_len = str(len(file_name))
sock.sendall(file_name_len.zfill(2))
sock.sendall(file_name)
sock.sendall(file_data)
print file_name + " successfully sent!"
sock.close()

Ass3 PL-1 university of pune

import java.sql.*;

class Ass3{
   
    public static void main(String args[]){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data1","user1","user1");
        System.out.println("Connection Success");
        PreparedStatement ps = conn.prepareStatement("CREATE TABLE COMP (NAME VARCHAR(20),QTY INT(5),PRICE DOUBLE(10,4))");//CREATE TABLE table_name (column_name column_type);
        ps.executeUpdate();
        System.out.println("Tbale created and data is inserting ...");
        PreparedStatement ps1 = conn.prepareStatement("INSERT INTO COMP VALUES(?,?,?)");//ps1
        //Set the values that will replace quesetion marks
        String name="DELL";
        int qty =1;
        double price= 45000.45;
        // now replace ?'s of PS1
        ps1.setString(1,name);
        ps1.setInt(2,qty);
        ps1.setDouble(3,price);
        ps1.executeUpdate();
        System.out.println("values are inserted ....");
        PreparedStatement ps2 = conn.prepareStatement("CREATE INDEX COMP1 ON COMP('NAME')");//ps1
        ps2.executeUpdate();
        System.out.println("INDEX ....");
       
        PreparedStatement ps3 = conn.prepareStatement("SHOW INDEX FROM COMP");//ps1
        ResultSet rs= ps3.executeQuery();
        while(rs.next())
        {
           
            System.out.println(rs.getString("Column_name"));
        }
       
        PreparedStatement ps4 = conn.prepareStatement("CREATE VIEW view AS SELECT PRICE,NAME FROM COMP");//ps1
        ps4.executeUpdate();
       
        PreparedStatement ps5 = conn.prepareStatement("SELECT * FROM view");//ps1
        ResultSet rs2= ps5.executeQuery();
        while(rs2.next())
        {
            System.out.print(rs2.getDouble("PRICE"));
            System.out.println(rs2.getString("NAME"));
        }
     }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
    } 
}

Tuesday 22 September 2015

Inode in python

import os,sys
print 'Enter file name'
name=raw_input()
fd=os.open(name,os.O_RDONLY)
info=os.fstat(fd)
print "The file Information is as follows:"
print "File inode number:",info.st_ino
print "Device number:",info.st_dev
print "Number of link:",info.st_nlink
print "UID of the file:%d" %info.st_uid
print "GID of the file:%d" %info.st_gid
print "Size:",info.st_size
print "Access time:",info.st_atime
print "Modify time:",info.st_mtime
print "Change time:",info.st_ctime
print "Protection:",info.st_mode
print "Number of blocks allocated:",info.st_blocks
print "Size of blocks:",info.st_blksize

os.close(fd)

Jdbc Connection test

import java.sql.*;

class Ass1
{
    public static void main(String args[])
    {
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data1","user1","user1");
        System.out.println("Connection Success");
        conn.close();
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
   
    }


}

JDBC-EAsy codes Ass1 -PL1

//content is wrapped so actual see that below comments must remain comments
import java.sql.*;

class Ass1_final
{
    public static void main(String args[])
    {
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data1","user1","user1");
        System.out.println("Connection Success");
        PreparedStatement ps = conn.prepareStatement("CREATE TABLE COMP (NAME VARCHAR(20),QTY INT(5),PRICE DOUBLE(10,4))");//CREATE TABLE table_name (column_name column_type);
        ps.executeUpdate();
        System.out.println("Tbale created and data is inserting ...");
        PreparedStatement ps1 = conn.prepareStatement("INSERT INTO COMP VALUES(?,?,?)");//ps1
        //Set the values that will replace quesetion marks
        String name="DELL";
        int qty =1;
        double price= 45000.45;
        // now replace ?'s of PS1
        ps1.setString(1,name);
        ps1.setInt(2,qty);
        ps1.setDouble(3,price);
        ps1.executeUpdate();
        System.out.println("values are inserted ");
       
        PreparedStatement ps2 = conn.prepareStatement("UPDATE  COMP SET QTY=QTY+1 WHERE NAME='DELL' ");//update
        ps2.executeUpdate();
        System.out.println("Qty updated by 1 ...");
       
        PreparedStatement ps3 = conn.prepareStatement("SELECT * FROM COMP");//display......
        ResultSet rs=ps3.executeQuery();
       
        while(rs.next())
        {
                System.out.print("NAME" + "\t"+rs.getString("NAME"));
                System.out.print("QTY" + "\t"+rs.getInt("QTY"));
                System.out.println("NAME" +"\t"+ rs.getDouble("PRICE"));
        }
       
        PreparedStatement ps4 = conn.prepareStatement("UPDATE  COMP SET QTY=QTY+1 WHERE NAME='DELL' ");//update
        ps4.executeUpdate();
       
        PreparedStatement ps5 = conn.prepareStatement("DROP TABLE COMP ");//update
        ps5.executeUpdate();
       
        System.out.println("table dropped or deleted..");
       
       
        rs.close();
        conn.close();
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
   
    }


}

Saturday 5 September 2015

Register -how to add a entry to a database using a jsp form and a servlet


watch the video


newjsp2.jsp

<%--
    Document   : newjsp
    Created on : 2 Sep, 2015, 10:25:39 PM
    Author     : harsh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Login page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <h2>Register</h2>
            <form action="NewServlet1" method="post">
                Email:<input type="email" name="email"><br>
                Name :<input type="text" name="name"><br>
                PASS :<input type="password" name="pass"><br>
                <input type="submit" name="submit" value="submit">
            </form>
        </div>
    </body>
</html>

NewServlet1.java

// a pure processing servlet to authenticate the user
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class NewServlet1 extends HttpServlet
{
   /// private static final String url = "jdbc:mysql://localhost/vusers";

    //    private static final String user = "root";

      //  private static final String pw = "mysql";
           
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/users","hall","bed");
            PreparedStatement ps = conn.prepareStatement("INSERT INTO USERNAME VALUES (?,?,?)");
            // get data of form
                        String email=req.getParameter("email");
            String username = req.getParameter("name");
            String password = req.getParameter("pass");
            // set the values for parameters
            ps.setString(1,username);
            ps.setString(2,password);
                        ps.setString(3, email);
            ps.executeUpdate();//database is updated
                //HttpSession session = req.getSession(true);
                                //session.setAttribute("Welcome",username);
                res.sendRedirect("newjsp.jsp");  // Wel is
               
       
               
        }
        catch (Exception e)
        {
                    res.sendRedirect("newjsp1.jsp");
            System.out.println(e.getMessage());
        }
    }
}
       


       

JSP Servlet Databse LoginPage

 Watch this video
newjsp.jsp

<%--
    Document   : newjsp
    Created on : 2 Sep, 2015, 10:25:39 PM
    Author     : harsh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Login page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <form action="log" method="post">
                Name :<input type="text" name="name"><br>
                PASS :<input type="password" name="pass"><br>
                <input type="submit" name="submit" value="submit">
            </form>
        </div>
    </body>
</html>

NewServlet.java

// a pure processing servlet to authenticate the user
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class NewServlet extends HttpServlet
{
   /// private static final String url = "jdbc:mysql://localhost/vusers";

    //    private static final String user = "root";

      //  private static final String pw = "mysql";
           
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/users","hall","bed");
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM USERNAME WHERE username=? AND password=?");
            // get data of form
                       
            String username = req.getParameter("name");
            String password = req.getParameter("pass");
            // set the values for parameters
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            if (rs.next())
            {
                HttpSession session = req.getSession(true);
                                session.setAttribute("Welcome",username);
                res.sendRedirect("index.html");  // Wel is
               
            }
            else
                            res.sendRedirect("newjsp1.jsp");
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}
       


       

Wednesday 2 September 2015

Jsp Login Example

copy paste the code below

newjsp.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Login page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <form action="log" method="post">
                Name :<input type="text" name="name"><br>
                PASS :<input type="password" name="pass"><br>
                <input type="submit" name="submit" value="submit">
            </form>
        </div>
    </body>
</html>

NewServlet.java


// a pure processing servlet to authenticate the user
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import java.sql.*;

public class NewServlet extends HttpServlet
{
           
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
        try
        {
            // get data of form
                       
            String username = req.getParameter("name");
            String password = req.getParameter("pass");
  
          
            if (username.equals("student") && password.equals("student"))
            {
              
                res.sendRedirect("index.html");                 
            }
            else
                            res.sendRedirect("newjsp1.jsp");
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}
        

Monday 3 August 2015

Disadvantages of IOT


Automation cool ha... but every automation is possible because of power/electricity/energy.

DATA COMMUNICATION  and Wireless Networks have a huge drawback that is

1,SLEEP when not in use
2. WakeUp to a response

The transaction from sleep to wakeup is where the lots of energy is reqired by the sensors.

Energy Harvesting is a real challenge in case of IOT circuits .Here is a doc  that will enahance your view over this disadvantage. 

Arduino IOT

1.Arduino UNO(ref-arduino.cc)
 Arduino/Genuino Uno is a microcontroller board based on the ATmega328P (datasheet). It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.. You can tinker with your UNO without worring too much about doing something wrong, worst case scenario you can replace the chip for a few dollars and start over again.
"Uno" means one in Italian and was chosen to mark the release of Arduino Software (IDE) 1.0. The Uno board and version 1.0 of Arduino Software (IDE) were the reference versions of Arduino, now evolved to newer releases. The Uno board is the first in a series of USB Arduino boards, and the reference model for the Arduino platform; for an extensive list of current, past or outdated boards see the Arduino index of boards.
You can find here your board warranty informations
CODING :_
void setup(){
// all declaration asre done a
}
 void loop(){//any thing you want to work all over again 
// eg- blink of led
}a single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools. The design is based on a Broadcom BCM2835 system on a chip (SoC), which includes an ARM1176JZF-S 700MHz processor, VideoCore IV GPU, and 512 megabytes of RAM. The design does not include a built-in hard disk or solid-state drive, instead relying on an SD card ooting and long-term storage - 

Introduction

HI friends this blog is all about  Internet of things.
AS if we want many of our daily simple tasks to be completed in an automated manner.
  • Home automation 
  • Agriculture 
  • Military Applications

Any thing you think needs a loop to run IOT is the answer to it.

Windows 10 -recover 25gb or more disk space in 5minutes