Skip to main content

Posts

Showing posts with the label python

Python Pipe

Pipes can be used in python using the popen command. os.popen(command[, mode[, bufferSize]]). The default mode is read which can also be explicitly indicated by "r" as shown in the example below. The parameter bufferSize can be 0 (no buffering), 1 (line buffering), negative(system default buffering) and positive values greater than 1 (number defines the buffer size to be used). #Open a pipe to or from command import os # shell command to show all the files in current directory with ".py" in the file name shellCommand = ' ls | grep .py' pipe = os.popen(shellCommand,"r") while True: # read each output line line = pipe.readline() if not line: break print 'outputLine: ' print line

calculate hop count in a graph using python

The following program finds out the hop count of the nodes in a connected network. Hop Count means the number of point to point connections in a graph of network. For example, in the graph shown below the hop count from node a to node b is one, node b to node c is two, etc. This program assumes the graph given above. usage : getHopCount(dictNeigh, node1,node2) Where:   dictNeigh : dictionary of list of neighbor of each node in the network node1 and node2 are the nodes for which hop count is calculated by the program Exception: if the nodes are not connected then it returns Zero import copy dictNeigh = dict() # dictNeigh defines the neighbors dictNeigh['a'] = ['b','c','d'] # neighbors of 'a' dictNeigh['b'] = ['a']# neighbors of 'b' dictNeigh['c'] = ['a','d'] dictNeigh['d'] = ['a','c','e'] dictNeigh['e'] = ['d','f'] dictN...

python code for querying own IP address

The  socket.gethostname() function is supposed to give the IP address of the machine but it may not give the real IP address but gives 127.0.0.1 . Even it is said that removing 127.0.0.1 from the /etc/hosts file (in ubuntu) can give the real IP but it could not help in my case. import socket nodeName = socket.gethostname() info = socket.getaddrinfo(nodeName, None, 0, socket.SOCK_STREAM) print [x[4][0] for x in info]   # prints 127.0.0.1 I came through another way which can print the real IP as well: import commands NodeIPS = commands.getoutput("/sbin/ifconfig | grep -i \"inet\" | grep -iv \"inet6\" | " +                          "awk {'print $2'} | sed -ne 's/addr\:/ /p'") print NodeIPS  # prints 127.0.0.1 and realIP(eg. 202.145.12.22) Note: code worked for ubuntu 12.04 and python 2.7 Sometimes its handy to have the IP address as list items. The following code does the work. impo...

Python : split any file (binary) to different pieces and join them

This python program takes an input file and then splits it into different smaller chunks. Next it can collect all the different chunks and join it to get the original file. -------------------------------------------------------- # define the function to split the file into smaller chunks def splitFile(inputFile,chunkSize): #read the contents of the file f = open(inputFile, 'rb') data = f.read() # read the entire content of the file f.close() # get the length of data, ie size of the input file in bytes bytes = len(data) #calculate the number of chunks to be created noOfChunks= bytes/chunkSize if(bytes%chunkSize): noOfChunks+=1 #create a info.txt file for writing metadata f = open('info.txt', 'w') f.write(inputFile+','+'chunk,'+str(noOfChunks)+','+str(chunkSize)) f.close() chunkNames = [] for i in range(0, bytes+1, chunkSize): fn1 = "chunk%s...