Wednesday, October 29, 2014

Use Python for Socket Programming to connect two or more PCs to share a text file.

Problem Statement:

Use Python for Socket Programming to connect two or more PCs to share a text file.

PROGRAM

serverm.py

import socket
import threading


#works for 5 times client trying to connect to it.After 5 clients have accessed it, server stops.
class sc(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        threadlock.acquire()
        ch=client.recv(1111)           #RECEIVE choice entered by client

        if ch=='1':                     #client is writing to the server file, temp.txt
            f=open('temp.txt','a+')
            r=client.recv(4096)
            f.write(r+'\n')
            f.close()
        else:
            f=open('temp.txt','a+')      #sending file data to the client from server file.
            msg=f.read()
            client.send(msg)
            f.close()
        threadlock.release()
threadlock=threading.Lock()
s=socket.socket()
s.bind(('localhost',9998))
s.listen(5)                              #5 is the mac number of connections that can be queued for this process
for i in range (0,5):
    client,client_addr=s.accept()
    s1=sc()
    s1.start()
    s1.join()
client.close()
s.close()


clientm.py

import socket
c=socket.socket()

c.connect(("localhost",9998))
ch=raw_input("Enter choice: 1 Write  2 Read\n")
c.send(ch)
if ch=='1':
    print "Writing to the file...\n"
    msg=raw_input("-->")
    c.send(msg)
else:
    print("file contents are...\n")
    msg=c.recv(4096)
    client_file=open('temp_copy.txt','w+')
    client_file.write(msg+'\n')
    client_file.close()
    print(msg)
c.close()

    

Write an IPC program using pipe. Process A accepts a character string and Process B inverses the string. Pipe is used to establish communication between A and B processes using Python or C++.

Problem Statement:

Write an IPC program using pipe. Process A accepts a character string and Process B inverses the string. Pipe is used to establish communication between A and B processes using Python or C++.

PROGRAM

Unnamed Pipe

#!/usr/bin/python
import os,sys

class npipe:
        def unnamed(self):
                fdr,fdw=os.pipe()
                procid=os.fork()
                if procid:
                    os.close(fdw)
                    fdr=os.fdopen(fdr)
                    str=fdr.read()
                    str=str[::-1]
                    print "reversed string: ",str
                    sys.exit(0)
                else:
                    os.close(fdr) 
                    str1=raw_input("Enter string: ")
                    fdw=os.fdopen(fdw,'w')
                    fdw.write(str1)
                    fdw.close()
                    sys.exit(0)

ob=npipe()
ob.unnamed()

Named Pipe

n_1.py

#!/usr/bin/python
import os
import sys

class a5_1:
        fd=0
        pipename="pipe3"

        def create_pipe(self):
                os.mkfifo(self.pipename)

        def write_pipe(self):
                self.fd=open(self.pipename,'w')
                self.fd.write("1")
                self.fd.close()
                str=raw_input("Enter string: ")
                self.fd=open(self.pipename,'w')
                self.fd.write(str)
                self.fd.close()

obj1=a5_1()
obj1.create_pipe()

while True:
    print "1.Enter string  2.Exit"
    choice=raw_input("Enter choice: ")
    if choice=='1':
        obj1.write_pipe()
    else:
        obj1.fd=open(obj1.pipename,'w')
        obj1.fd.write("2")
        obj1.fd.close()
        os.unlink(obj1.pipename)

        break;

n_2.py

#!/usr/bin/python
import os
import sys

class a5_2:
        fd=0
        pipename="pipe3"
        def read_pipe(self):
                self.fd=open(self.pipename,'r')
                stri=self.fd.read()
                revstr=stri[::-1]
                print "reveresed string: ",revstr
                self.fd.close()

obj2=a5_2()
while True:
    obj2.fd=open(obj2.pipename,'r')
    choice=obj2.fd.read()
    if choice=='1':
        obj2.read_pipe()
    else:
        break


Write a program in Python/C++ to read display the i-node information for a given text file, image file.

Problem Statement:
Write a program in Python/C++ to read display the i-node information for a given text file, image file.

PROGRAM


#include <iostream>
#include <sys/stat.h>
using namespace std;

class inode
{
    struct stat s;
    char str[1000];
public:
    void accept()
    {
     cout<<"Enter the path of the file:\t";
     cin>>str;
    }
    void information()
    {
    int d=stat(str,&s);
    }
    void disp()
    {
     cout<<"\nThe inode details of the file:\n";
     cout<<"\n\t 1)Device ID: "<<s.st_dev;
     cout<<"\n\t 2)File Serial No: "<<s.st_ino;
     cout<<"\n\t 3)Mode : "<<s.st_mode;
     cout<<"\n\t 4)No of Hard Links: "<<s.st_nlink;
     cout<<"\n\t 5)User ID: "<<s.st_uid;
     cout<<"\n\t 6)Group ID: "<<s.st_gid;
     cout<<"\n\t 7)Size: "<<s.st_size;
     cout<<"\n\t 8)Time of last access: "<<s.st_atime;
     cout<<"\n\t 9)Time of last data modification: "<<s.st_mtime;
     cout<<"\n\t 10)Time of last status change: "<<s.st_ctime;
     cout<<"\n\t 11)I/O block size: "<<s.st_blksize;
     cout<<"\n\t 12)No of blocks allocated: "<<s.st_blocks<<endl;
    }

};

int main()
{
    inode node;
    node.accept();
    node.information();
    node.disp();

    return 0;
}


OUTPUT

[dbsl@localhost ~]$ g++ A4_inode.cpp
[dbsl@localhost ~]$ ./a.out

     Enter path: /home/dbsl/A4_inode.cpp

     Filename : /home/dbsl/A4_inode.cpp

     Device id : 2058
     Inode number : 526528
     Device id (if the file is character or block special) : 0
     File size :2132
     Block size : 4096
     Number of blocks allocated for this file : 8
     Last access time : 1406093149 s
     Modified time : 1405655410 s
     Number of hard links : 1

     User name : dbsl
     User id : 1000

     Group name : dbsl
     Group id : 1000

     Mode : -rw-r--r--


Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...