About Me

My photo
Working as Technical Lead in CollabNET software private limited.

Monday, 26 October 2009

Python socket programming

I was in a situation to make the particular port listen,

Below code helped me in doing that simple.

import socket

#create an INET, STREAMing socket
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
#bind the socket to a public host,
# and a well-known port
serversocket.bind((socket.gethostname(), 80))
#become a server socket
serversocket.listen(5)

Thursday, 24 September 2009

To touch a folder contents recursive

Some times you may need to alter the access date of files and folder. You can do it by a recursive touch command.

find . | xargs touch

psql output to file

Below is the command to redirect psql output to a file.

psql> \o /tmp/file.txt
psql> select * from table

This should allow output to custom file.

Friday, 18 September 2009

Removing control characters - python

You want a regex to remove control characters (< chr(32) and > chr(126)) from
strings ie.

line = re.sub(r"[^a-z0-9-';.]", " ", line) # replace all chars NOT A-Z,a-z, 0-9, [-';.] with " "

1. What is the best way to include all the required chars rather than list them all within the r"" ?

You have to list either the chars you want, as you have done, or the
ones you don't want. You could use
r'[\x00-\x1f\x7f-\xff]' or
r'[^\x20-\x7e]'

line = re.sub(r'[\x00-\x1f\x7f-\xff]', "", "test ^A ^B testing this again")

Intresting tip abt tcpdump

Searched for a tool like wireshock in windows and came to know this!. This help me to dump the soap requests.

Here is the simple command.

sudo tcpdump -XX -s0 -i lo -w k "tcp port 80"

-XX: Print packets in hex and ASCII
-s0: Print whole packets, not only first 68 chars.
-i is the interface to monitor.

"tcp port 80" Filter expression.
-w output file.


This dumps all the requests and responses which are passing through tcp port 80!.