Archive for July, 2007

a script to write all code snippets to a database

July 10, 2007

Since I have started seeing nice little code snippets around the web that I would like to keep somewhere as a reference, but not have in a program that I would have to search through, I decided to write a little script to save all those snippets to a database that I could retrieve later. Please check it out. It uses the new module mod_python that I recently installed and with it I could do almost anything that php could do. If you try to click on the modify button, it will present itself with an authentication popup, which blocks anyone trying to put bullshit in there. In the future, I hope to explore mod_python more and become more powerful with it.

opening shell using subprocess

July 6, 2007

Opening a shell can be useful in some cases. For instance, imagine that you are on a different network and you would like to use sockets to control another computer. All you need to do is open up a pipe to a shell, with the input being read with socket.revc() and the the output going to socket.write(text). In such a way, it becomes like a telnet or ssh session, whereas it’s like you are sitting directly on the machine. I thought that using the module subprocess in python would be ideal for it. However, whenever I used Popen to open a shell, I couldn’t keep write on stdin and get stdout only once, and then I would get a ‘Broken Pipe Error’. After a long search on comp.lang.python, I finally discovered that I need to set up the file descriptors to be non-blocking in order for the connection not to close when there is no more data to be written to stdin. What happens is that once the pipe detects that there is no more data to be read from stdin, the pipe issues a EOF and the pipe closes. Here is a link to the discussion on a forum: click