softwarelabcs699
Software Lab CS699
All I need for software lab
Python
Server-Client
Simple UDP Server
#Creating a UDP Connectionless Socket Server
from socket import socket, AF_INET, SOCK_DGRAM
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('127.0.0.1', 12365))
data, addr = s.recvfrom(1024)
print "Client said: ", data
Simple UDP Client
#Creating a UDP Connectionless Socket Client
from socket import socket, AF_INET, SOCK_DGRAM
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('127.0.0.1', 0))
server=('127.0.0.1', 12365)
s.sendto("Hello, I am here!", server)
Simple TCP Server
#Creating a TCP Connection-oriented Socket Server
from socket import socket, AF_INET, SOCK_STREAM
try:
s = socket(AF_INET, SOCK_STREAM)
s.bind(('127.0.0.1', 12365))
s.listen(5)
except:
print "Socket error."
exit(1)
sock, addr = s.accept()
data = sock.recv(1024)
print "Client said: ", data
Simple TCP Client
#Creating a UDP Connectionless Socket Client
from socket import socket, AF_INET, SOCK_STREAM
s = socket(AF_INET, SOCK_STREAM)
s.connect(('127.0.0.1', 12365))
s.send("Hello, I am here!")
String functions
int index = s.find('<rec>') #Finds the given string (<rec>) in the string s and return its index.
http://www.devshed.com/c/a/Python/String-Manipulation/
if str[-1] == '.': # checks for last character in the string.
With RegExp, it all becomes more powerful
import re
Compiled_RegExp = re.compile('(<[^/].*?>)', re.IGNORECASE)
Tag_Start = Compiled_RegExp.search(html_body)
IsHeader = Compiled_RegExp_Headers.match(Tag_Start.group())
http://www.amk.ca/python/howto/regex/
To spawn a process from python,
import sys
import os
import subprocess
processcall='./findservice.sh ' #This is the process I wish to start
subprocess.call([processcall],shell=True)
Define functions using
def getRange(Range): #return parameters are not shown in the def
return Start, End #and return like this!
so the call requires to be something like
PortStart, PortEnd = getRange(PortRange)
Unique elements are added in a mathematical set. Python is really rich
from sets import Set
unique=Set()
unique.add(RequiredMonth) # if the month repeats, nothing is added! http://docs.python.org/lib/types-set.html http://docs.python.org/lib/typesseq-mutable.html
Checking and using command line params
if len(sys.argv) == 4:
host = sys.argv[1]
port = sys.argv[2]
Loops
while len(s)>0:
for w in range(int(WStart), int(WEnd)+1):
And Arrays are great. Have a look.
http://pentangle.net/python/handbook/node39.html
http://docs.python.org/lib/module-array.html
Unix
Starting from conditions
if [ $# -ne 1 ]; then #-ne used for numeric values $# means number of command line params
fi
Here is a full guide! http://www.scit.wlv.ac.uk/cgi-bin/mansec?1+test
Looping constructs. Line wise read from a file.
for iMonth in 2 3 4 5 6 7 8 9 10 11 12 13
do
#Somethings
done
for X in *.html #looping on all .html files.
do
grep -L '<UL>' "$X"
done
The power of for loops is shown in http://www.selfseo.com/story-17966.php [The selfseo.com site may have been compromised. I have removed the hyperlink but kept the original page url. Use your discretion to go to this site.]
while read myline
do
#A lot of things
done <UsefulTable.txt
Functions can be defined using
function stol()
{ ... }
Many good things here http://tldp.org/LDP/abs/html/
Awk and regexp together
ls -all | awk '{if ($8 ~ /^\..*/) {printf "Hidden "; print} else {print;}}' #Prefix all hidden files with the word "Hidden". pattern matching in awk is simple $x ~ /pattern/ or $x !~ /pattern/
ls -all | awk 'BEGIN {tot=0; i=0} $1 !~ /^d/ {tot=tot+$5} END {while(tot>1024){tot=tot/1024; i=i+1} if(i==1){Gran="K"}else if(i==2){Gran="M"} else if (i==3) {Gran="G"} else {Gran="Too big"} print "Total:", tot, Gran}' #Great Idea to print the non-directory files in this directory,
Ashutosh Dhekne