WTF *Nix

Just another *nix Blog

Archive for the ‘Python’ Category

Oh WTF Time Is It?

May-8-2009 By WTF *Nix

It’s NTP Server Time!

So what is NTP you may ask? It’s what we call Network Time Protocol, which helps us all synchronize our *nix system’s clock with an accurate time source. There are a number of websites that allow the public to access and sync up with them. They are divided into two types Stratum 1 and Stratun 2

What are these so called Stratum’s you may ask?

Stratum 1 = NTP websites using an atomic clock for timing

Stratum 2 = NTP websites with slightly but accurately LESS time sources (NOT ACCURATE to the REAL ATOMIC CLOCKS)

You may get a list of available NTP Stratum type servers from: www.ntp.org

It is only a valid and good practice to keep at least one *nix server on your local network be tthe “local time server” for all of your other devices… This only makes and keeps the correlation of system events on different systems much easier to maintain a “central but local” time. It not only helps there but also helps in bandwidth usage, due to the NTP traffic and reduces the need to manage firewall rules for “EACH” of the NTP clients that you have running if you have more than one on your network…

Sometimes, (majority of the time) not all of your servers will have NET access which in such cases you’ll need a central “TIME SERVER” / “SERVER” that any and all can access off your local network. That being said you can have a “Gateway” server to do all this for you if configured properly and accordingly.

That’s all for now on the NTP servers, it’s time to head out and party in Seattle it’s 9:56:44PM PST per my NTP server, and I will be back later to discuss on how the ntp.conf file works :) But you need to get ntp yourself from a reliable source if you don’t have it installed on your server already… Use Google.com to find it for your *nix flavor.

Share and Enjoy:
  • Google Bookmarks
  • MySpace
  • Facebook
  • StumbleUpon
  • Print
  • email
  • Digg

{Upgrade Yum} FC 10 to FC 11 Preview

May-4-2009 By WTF *Nix

Before I get into the instructions, let’s say I’m one pleased puppy on this new release…

This has to be the best Fedora Release yet! Normally I don’t reboot, but after seeing what happened to my last reboot here on my gateway server out of the house, it took literally 10-12 seconds till I was serving the web and my server was back online after rebooting fedora 2x after upgrading to F 11 Leonidas… This is what’s really going to be the winner for any *nix flavor Workstation that loves rebooting fast, read more of this here
20SecondStartup

Oh and Python 2.6 finally??? :P Oh well I built it already and had my own RPM laying here, I’m just not looking forward to Python 3.0 quite yet… I wished Python would slow down some! Even though my bud loves the Python 3.0 which he’s a robot builder for a large firm in Japan.

Just like any other upgrade that you do with Yum, this is the most easiest!

Keep up to date on their final release here:  Fedora’s Leonidas Final Release

This is for their Preview Release Core 11

yum update

yum clean all

yum clean all (Just to be sure)

yum update (Just to be sure)

yum clean all (Just to be sure)

i386:

rpm -Uvh http://mirrors.usc.edu/pub/linux/distributions/fedora/linux/releases/test/11-Preview/Fedora/i386/os/Packages/fedora-release-10.92-1.noarch.rpm http://mirrors.usc.edu/pub/linux/distributions/fedora/linux/releases/test/11-Preview/Fedora/i386/os/Packages/fedora-release-notes-10.93.0-1.fc11.noarch.rpm

x86_64:

rpm -Uvh http://mirrors.usc.edu/pub/linux/distributions/fedora/linux/releases/test/11-Preview/Fedora/x86_64/os/Packages/fedora-release-10.92-1.noarch.rpm http://mirrors.usc.edu/pub/linux/distributions/fedora/linux/releases/test/11-Preview/Fedora/x86_64/os/Packages/fedora-release-notes-10.93.0-1.fc11.noarch.rpm

yum -y update

You may need to do some housekeeping to remove some packages to work out a few dependency issues, however mine was swift as I only build gateways and lite boxes separately for one for HTTP, one for MySQL, one for Qmail.

So remember use GOOGLE to research your ERRs, or post them in here, don’t guarantee me to keep checking every hour, someone may come along like billy boy gates or steve balmer from M$ and they may know the answer ;)

All the love to the *nix world keep awkin on!

Share and Enjoy:
  • Google Bookmarks
  • MySpace
  • Facebook
  • StumbleUpon
  • Print
  • email
  • Digg

Python is an extremely extensible language in that you can add new capabilities by importing modules.

Here are just a slim few, but a widely used all throughout the day to day process of programming within python, so here’s for anyone that needs a WTF Quick Card…

sys – Allows interaction with the Python system:

  • exit() – exit!
  • argv – access command line arguments
  • path – access the system module search path
  • ps1 – change the ‘>>>’ python prompt!

os – Allows interaction with the operating system:

  • name – the current operating system, useful for portable programs
  • system – execute a system command
  • mkdir – create a directory
  • getcwd – find the current working directory

re – Allows manipulation of strings with Unix style regular expressions

  • search – find pattern anywhere in string
  • match – find at beginning only
  • findall – find all occurences in a string
  • split – break into fields separated by pattern
  • sub,subn – string substitution

math – Allows access to many mathematical functions:

  • sin,cos etc – trigonometrical functions
  • log,log10 – natural and decimal logarithms
  • ceil,floor – ceiling and floor
  • pi, e – natural constants

time – time(and date) functions

  • time – get the current time (expressed in seconds)
  • gmtime – convert time in secs to UTC (GMT)
  • localtime – convert to local time instead
  • mktime – inverse of localtime
  • sleep – pause program for n seconds

random – random number generators – useful for games programming!

  • randint – generate random integer between inclusive end points
  • sample – generate random sublist from a bigger list
  • seed – reset the number generator key

Remember this is just a SLIM bit of what’s instore for you that Python has, there are literally dozens of modules given too you with Python, then plenty more you can download!

Remember that using these within your code, you need to first IMPORT them as such:

>>>import SimpleXMLRPCServer as s

>>>s.SimpleXMLRPCRequestHandler()

Notice the above example is a shorter way of calling this module rather than typeing out the FULL SimpleXMLRPCServer :) Just saves us time in typing, unless you have an IDE that does it all for you by autocompletion… I prefer VIM for my IDE… So WTF, get it done and >>>import WTFNix.com

Share and Enjoy:
  • Google Bookmarks
  • MySpace
  • Facebook
  • StumbleUpon
  • Print
  • email
  • Digg

Python URL Parsing

Mar-23-2009 By WTF *Nix

The urlparse module included with Python makes it easy to break down URLs into specific components and reassemble them. This is very useful for a number of purposes when processing HTML documents.

The urlparse(urlstring [, default_scheme [, allow_fragments]]) function takes the URL provided in urlstring and returns the tuple (scheme, netloc, path, parameters, query, fragment). The tuple can then be used to determine things such as location scheme (HTTP, FTP, and so on), server address, file path, and so on.

The urlunparse(tuple) function accepts the tuple (scheme, netloc, path, parameters, query, fragment) and reassembles it into a properly formatted URL that can be used by the other HTML parsing modules included with Python.

The urljoin(base, url [, allow_fragments]) function accepts a base URL as the first argument and then joins whatever relative URL is specified in the second argument. The urljoin function is extremely useful in processing several files in the same location by joining new filenames to the existing base URL location.

Try this example out within your PY environment yourself and see:

import urlparse
parsedTuple = urlparse.urlparse(
"http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
unparsedURL = urlparse.urlunparse((URLscheme, \
        URLlocation, URLpath, '', '', ''))
newURL = urlparse.urljoin(unparsedURL,
"/module-urllib2/request-objects.html")
Share and Enjoy:
  • Google Bookmarks
  • MySpace
  • Facebook
  • StumbleUpon
  • Print
  • email
  • Digg

VIM Power Editor Commands :: Part I

Mar-23-2009 By WTF *Nix

If you are looking to have a list of commands to keep handy, figured I would share these here with others that need a starting point with VIM:

1. The cursor is moved using either the arrow keys or the hjkl keys.
h (left)    j (down)       k (up)        l (right)

2. To start Vim from the shell prompt type:  vim FILENAME <ENTER>

3. To exit Vim type:       <ESC>   :q!     <ENTER>  to trash all changes.

OR type:   <ESC>   :wq     <ENTER>  to save the changes.

4. To delete the character at the cursor type:  x

5. To insert or append text type:

i   type inserted text   <ESC>        insert before the cursor
A   type appended text   <ESC>         append after the line

NOTE: Pressing <ESC> will place you in Normal mode or will cancel an unwanted and partially completed command.

Here is VIM’s Part II

Share and Enjoy:
  • Google Bookmarks
  • MySpace
  • Facebook
  • StumbleUpon
  • Print
  • email
  • Digg