About Me

My photo
Working as Technical Lead in CollabNET software private limited.
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, 10 June 2010

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)

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")