tag:blogger.com,1999:blog-79954562009-02-20T20:05:11.209-08:00Thoughts from Rob HudsonRob Hudsonhttp://www.blogger.com/profile/06500424283608711248noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-7995456.post-1107839574149202342004-11-24T12:00:00.000-08:002005-02-07T21:12:54.150-08:00watchI just learned a new unix command -- one I could have used many times in the past... watch.
<br />
<br />I've copied or removed large directories of files and have run 'df' many times in a row to either see how much was freed or see how close I was getting to a full disk. If I had done, 'watch df', it would have made it much easier. Now I know. :)<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7995456-110783957414920234?l=treborhudson.blogspot.com'/></div>Rob Hudsonhttp://www.blogger.com/profile/06500424283608711248noreply@blogger.com0tag:blogger.com,1999:blog-7995456.post-1092847963966106392004-07-26T20:00:00.000-07:002004-08-18T09:56:09.016-07:00OSCON day 1I'm going to try to blog my week at OSCON. Most of the tutorials or session I'll be attending will be on PHP, but I'll be attending a few others on MySQL, Subversion and Gentoo as well.
<br />
<br /><b>Object Oriented Programming with PHP5</b>
<br />
<br /><a href="http://conferences.oreillynet.com/cs/os2004/view/e_sess/4983">OOP with PHP5</a> was a discussion on object oriented programming (OOP) and how PHP5 implements various OOP ideas. Topics covered were: Classes and objects, methods and data, constructors/destructors, public/private/protected members and data, inheritance, abstract classes, interfaces, exceptions, and a few other OOP topics.
<br />
<br />While that was a nice overview of PHP5's object model, the tutorial ran long and we didn't cover some of what I thought would be the more interesting parts of the tutorial, such as:<ul><li> Automated Tools for creating PHP from UML. </li><li> SimpleXML </li><li> SOAP </li><li> How the object models opens interoperability with other langauges (Java/Python). </li></ul>
<br />In the end, the tutorial wasn't much more than what I've already read in <a href="http://www.php.net/zend-engine-2.php">this article</a> on PHP5's new features. Maybe the topic list was a little too ambitious, but I left wishing I could have heard more about the other topics, and like other tutorials, there was no booklet to accompany the tutorial to read what we missed.
<br />
<br /><b>PHP Security</b>
<br />
<br /><a href="http://conferences.oreillynet.com/cs/os2004/view/e_sess/5450">PHP Security</a> was an excellent tutorial by <a href="http://www.shiflett.org/">Chris Shiflett</a>. He gave a nice, thorough overview of various attacks against one's PHP applications, why they are a security concern, and how to program them in such a way as to defend against the various attacks.
<br />
<br />I have read a few of his articles in <a href="http://www.phparch.com/">PHP Architect</a> and I felt I learned a lot from this tutorial. I went in with a vague idea of a few of the attacks and left with a more in-depth knowledge of how the attacks work and how to fix my code.
<br /><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7995456-109284796396610639?l=treborhudson.blogspot.com'/></div>Rob Hudsonhttp://www.blogger.com/profile/06500424283608711248noreply@blogger.com0tag:blogger.com,1999:blog-7995456.post-1107839816083075522004-04-27T12:00:00.000-07:002005-02-07T21:17:32.600-08:00RSS delivered to your emailI recently found and setup <a href="http://www.aaronsw.com/2002/rss2email/">rss2email</a>.
<br />Check out the screenshot. :)
<br />
<br />I used to have different RSS feeds at work and home and found it hard to synchronize them, though I didn't try too hard. This is a nice solution since it is central, and I get new blog postings each morning (when I set up my cron job). Sweet!
<br />
<br /><span style="font-weight:bold;">Update:</span> I recently switched to <a href="http://newspipe.sourceforge.net/">newspipe</a> when I set up a new web/mail server. It saves its configuration file in an OPML format and has support for digests, HTML email, and many other feed options.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7995456-110783981608307552?l=treborhudson.blogspot.com'/></div>Rob Hudsonhttp://www.blogger.com/profile/06500424283608711248noreply@blogger.com0tag:blogger.com,1999:blog-7995456.post-1107840046199019752003-11-26T12:00:00.000-08:002005-02-07T21:21:11.700-08:00MySQL tipsI've been studying recently for the <a href="http://www.mysql.com/certification/index.html">MySQL certification</a> test and have discovered a few tricks along the way. One of my favorites is the combination of using pager and vertical output.
<br />
<br />To set your pager to use less:
<br /><pre>
<br />mysql> pager less
<br />PAGER set to less
<br /></pre>
<br />Now when you perform a select which returns more rows than rows on your screen, the pager will allow you to paginate through the results.
<br />
<br />To get MySQL to display results vertically instead of in columns horizontally, end your SQL with "\G" instead of with the semicolon (or "\g")...
<br /><pre>
<br />mysql> SELECT * FROM mails WHERE LENGTH(txt) < 300 lIMIT 300,1\G
<br />*************************** 1. row ***************************
<br /> msg_nro: 3068
<br /> date: 2000-03-01 23:29:50
<br />time_zone: +0200
<br />mail_from: Monty
<br /> reply: monty@no.spam.com
<br /> mail_to: "Thimble Smith"
<br /> sbj: UTF-8
<br /> txt: >>>>> "Thimble" == Thimble Smith writes:
<br />
<br />Thimble> Hi. I think this is a good idea. Is anyone familiar with UTF-8
<br />Thimble> or Unicode? Otherwise, I'll put this on my TODO list and see what
<br />Thimble> happens.
<br />
<br />Yes, please do that.
<br />
<br />Regards,
<br />Monty
<br /> file: inbox-jani-1
<br /> hash: 190402944
<br />1 row in set (0.09 sec)
<br /></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7995456-110784004619901975?l=treborhudson.blogspot.com'/></div>Rob Hudsonhttp://www.blogger.com/profile/06500424283608711248noreply@blogger.com0tag:blogger.com,1999:blog-7995456.post-1107839471001321342003-07-18T12:00:00.000-07:002005-02-07T21:11:11.000-08:00PyNewbie on making cryptogramsI used to enjoy cryptograms a lot and wanted to generate some on my own. I originally wrote a script to do this in Perl. While learning Python, I rewrote it and learned a few things about the string library in the process. Here goes...
<br />
<br />import sys, string, random
<br />
<br />We're going to be using sys to catch arguments passed to our script, or to catch input from a pipe. We've got lots of string manipulation so we're importing the string module. The random module will give us a way to shuffle a list so we can match each letter with another random letter. More on all of these later in the script.
<br />
<br />We'll jump to the bottom of the script and show something you'll see a lot in Python:
<br /><pre>
<br />if __name__ == '__main__':
<br /> if len(sys.argv) == 2:
<br /> print encrypt(open(sys.argv[1], 'r'))
<br /> else:
<br /> print encrypt(sys.stdin)
<br /></pre>
<br />The first line here tells Python to call the encrypt function if the name of the script is __main__; more technically, if the name of the module is __main__. Every Python script can be a module. If the module is imported from another Python script, the module's __name__ is set to the filename without the directory path or file extension. If the script is run directly from the command line as a standalone program, __name__ is set to __main__. In this way, Python makes it easy to make programs reusable, or debug parts of a module before integrating it into a larger program.
<br />
<br />The rest of this segment checks the number of arguments passed in. The first argument is always the script name itself. If there is a 2nd argument, we open the file and pass the file object to the encrypt function. If there isn't a 2nd argument, we open sys.stdin which is intended to let this script run by piping the output of another program to this program. This allows for running it using fortune: fortune | ./cryptogram.py
<br /><pre>
<br /> 1: def encrypt(file):
<br /> 2: alphabet = string.uppercase
<br /> 3: text = ''
<br /> 4:
<br /> 5: for line in file.readlines():
<br /> 6: text += string.upper(line)
<br /> 7:
<br /> 8: mixed = list(string.uppercase)
<br /> 9: random.shuffle(mixed)
<br />10: mixed = string.join(mixed,'')
<br />11:
<br />12: return string.translate(text,string.maketrans(mixed,alphabet))
<br /></pre>
<br />This segment defines a function encrypt and takes one parameter, a file object. Line 2 sets up our alphabet, which is preset in the string library, along with many others. Lines 5 and 6 read the file in one line at a time, converts it to all uppercase, and appends each line onto the text variable.
<br />
<br />Now we get to the meat. Lines 8 through 10 set up a string of letters consisting of the alphabet in random order. Line 8 uses the uppercase variable of the string module like we used before, but puts each letter into a list. The shuffle method of the random module expects a list so we pass 'mixed' to that. It performs an in-place shuffle. Line 10 we convert the list of letters back into a string.
<br />
<br />Line 12 calls the translate function. This would be similar to Perl's tr/// call. The first argument is the text file we want to translate. The second argument is the translation table, which is a 256 byte string of the mapping that needs to be done. The maketrans function helps us build that string and both arguments to maketrans need to be the same length.
<br />
<br />With that, I'll leave you with the full script, and the output on a quote. Here's a hint, it's a Monty Python quote.
<br /><pre>
<br />#!/usr/bin/env python
<br /># cryptogram.py
<br />import sys, string, random
<br />
<br />def encrypt(file):
<br /> alphabet = string.uppercase
<br /> text = ''
<br />
<br /> for line in file.readlines():
<br /> text += string.upper(line)
<br />
<br /> mixed = list(string.uppercase) # list of letters in alphabet
<br /> random.shuffle(mixed) # randomize it
<br /> mixed = string.join(mixed,'') # convert back to string
<br />
<br /> return string.translate(text,string.maketrans(mixed,alphabet))
<br />
<br />if __name__ == '__main__':
<br /> if len(sys.argv) == 2:
<br /> print encrypt(open(sys.argv[1], 'r'))
<br /> else:
<br /> print encrypt(sys.stdin)
<br /></pre>
<br />Output...
<br /><pre>
<br />R GTRAN GTDG DSS OYYI, BROTG GTRANRAO CMYCSM RA
<br />GTRW ZYVAGBL DBM WRZN DAI GRBMI YF EMRAO GYSI
<br />GTDG DSS OYYI, BROTG GTRANRAO CMYCSM RA GTRW
<br />ZYVAGBL DBM FMI VC URGT EMRAO GYSI GTDG DSS OYYI,
<br />BROTG GTRANRAO CMYCSM RA GTRW ZYVAGBL DBM FMI
<br />VC URGT EMRAO WRZN DAI GRBMI. R'J ZMBGDRASL AYG,
<br />DAI R'J WRZN DAI GRBMI YF EMRAO GYSI GTDG R DJ!
<br /> -- JYAGL CLGTYA
<br /></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7995456-110783947100132134?l=treborhudson.blogspot.com'/></div>Rob Hudsonhttp://www.blogger.com/profile/06500424283608711248noreply@blogger.com0tag:blogger.com,1999:blog-7995456.post-1107839306627845752003-07-13T12:00:00.000-07:002005-09-21T12:37:52.270-07:00PyNewbie on Using Sockets in PythonA socket is a connection from one program to another. The two programs are typically on different machines, but they can also be on the same machine. Most sockets are from a client program connect to a server program to get some sort of information. Most socket transmissions are bi-directional -- send a request, receive some information. When you request a web page from within a web browser, you are causing the initiation of a socket connect from your computer (the client) to the web server's computer (the server, which could also be located on the same machine). You request a particular web page and the server responds with the information which the web browser displays as the web page.<br /><br />To create a connection in Python, one imports the built-in socket module, creates a sockect object, and calls the methods to establish a connection, send and receive data, etc. To import the socket module and create a socket object looks like this:<br /><br /><pre>from socket import *<br />sockobj = socket(AF_INET, SOCK_STREAM)<br /></pre><br />The first line imports the socket module. The next line creates the socket object. Using the 'AF_INET' and 'SOCK_STREAM' together tells the socket module to create a TCP/IP socket.<br /><br />The next step would be to connect to a server and send or receive information...<br /><br /><pre>sockobj.connect(("128.59.16.20", 13))<br />sockobj.send("Python User")<br />data = sockobj.recv(1024)<br /></pre><br />In this segment, we connect to a time server at IP address 128.59.16.20 on port 13, the typical 'daytime' service port. We sent a name to the server for identification purposes only using the send method. Some time servers require information sent and some don't, while some will only allow certain clients to connect to them. Finally, we received the time data from the server.<br /><br />The data is small and we didn't need to create a loop to receive data larger than the 1024 bytes. <em>sockobj.recv(1024)</em> reads at most 1024 bytes of data and returns it as a string. If the data is longer than 1024 bytes a loop is required to get the rest of the data. When no more data is available, an empty string is returned signifying the end of the data stream.<br /><br />I supposed you'd like to see the response, so we'll print what we got.<br /><pre><br />print 'response was:', data<br />sockobj.close()<br /></pre><br />Here we simply printed the data and closed the socket connection.<br /><br />The complete script is the following:<br /><br /><pre><br />from socket import *<br />sockobj = socket(AF_INET, SOCK_STREAM)<br />sockobj.connect(("128.59.16.20", 13))<br />sockobj.send("Python User")<br />data = sockobj.recv(1024)<br />print 'response was:', data<br />sockobj.close()<br /></pre><br /><br />The result I got when I ran the following was:<br /><pre><br /># python pysocket.py<br />response was: Mon Jul 14 02:02:56 2003<br /></pre><br /><br />That concludes my initial small Python scripting lesson. It was only an exercise for me to learn more about Python. If motivated (or I get feedback), I hope to write more as I learn more about Python.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7995456-110783930662784575?l=treborhudson.blogspot.com'/></div>Rob Hudsonhttp://www.blogger.com/profile/06500424283608711248noreply@blogger.com1