Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Darren Paul Griffith http://madphilosopher.ca/

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Python CGI script to display client's IP address

When run as a cgi script, this will print the client's IP address.

   1  
   2  import cgi
   3  import os
   4  
   5  print "Content-type: text/html"
   6  print ""
   7  
   8  print cgi.escape(os.environ["REMOTE_ADDR"])

Filter all URLs from standard input

Code:

Put the following in a file named url_scrape.py.
   1  
   2  #!/usr/bin/env python
   3  '''Prints a list of URLs that are found in standard input.
   4  
   5  It will only find URLs between quotes ("" or '') and starting with http://
   6  '''
   7  
   8  import re
   9  import sys
  10  
  11  # Pattern for fully-qualified URLs:
  12  url_pattern = re.compile('''["']http://[^+]*?['"]''')
  13  
  14  # build list of all URLs found in standard input
  15  s = sys.stdin.read()
  16  all = url_pattern.findall(s)
  17  
  18  # output all the URLs
  19  for i in all:
  20      print i.strip('"').strip("'")


Example Usage:
   1  
   2  wget -O - http://madphilosopher.ca/ | ./url_scrape.py | sort | uniq

Scan the FreeBSD ports collection and print one-line summary for each port

The FreeBSD ports collection is a skelton of installable software on a local FreeBSD machine. This small utility lets you scan the collection by category, to find out what software is available in that category. Call the script portinfo.py.

A deeper explanation of the script can be found here.

   1  
   2  """Scans a directory under /usr/ports/ and gives the portname and comment for each port found."""
   3  
   4  import os
   5  import os.path
   6  import sys
   7  
   8  def findmakefiles(curdir):
   9      '''goes one level deep under curdir to find and act on the Makefiles.'''
  10  
  11      for name in os.listdir(curdir):
  12          pathname =  os.path.join(curdir,  name, 'Makefile')  # COMMENTs are found in the Makefiles
  13          if os.path.isfile(pathname):
  14              processmakefile(name, pathname)
  15  
  16  def processmakefile(name, pathname):
  17      '''grabs the port's COMMENT from the Makefile.'''
  18      f = open(pathname)
  19      data = f.readlines()
  20      for line in data:
  21          if line[0:7] == 'COMMENT':
  22              try:
  23                  comment = line.strip().split('\t')[1]   # most COMMENTs are tab delimited
  24              except:
  25                  comment = line.strip()                  # some are not, so just print the whole line
  26              print "%-15s  %s" % (name, comment)
  27  
  28      f.close()
  29  
  30  def usage():
  31      print 'Usage: portinfo.py /usr/ports/[category]'
  32      sys.exit(1)
  33  
  34  
  35  if __name__ == '__main__':
  36  
  37      # get directory from command line
  38      args = sys.argv[1:]
  39      if len(args) == 1:
  40          curdir = args[0]
  41      else:
  42          usage()
  43      
  44  ##    curdir = '/usr/ports/security/'
  45   
  46      # scan this directory   
  47      if os.path.isdir(curdir):
  48          findmakefiles(curdir)
  49      else:
  50          sys.stdout = sys.stderr
  51          print "'%s' is not a valid directory." % curdir
  52          usage()

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS