CGI Proplem displaying image

Georgy no.mail at available.net
Tue May 11 18:32:09 EDT 2004


"matthiasjanes" <matthiasjanes at gmx.net> wrote in message news:d588131f.0405102237.15106ea5 at posting.google.com...
> matthiasjanes at gmx.net (matthiasjanes) wrote in message
> news:<d588131f.0405090912.560b51db at posting.google.com>...
>
> BUT What I want is that I display the image on an HTML page with text
> together.


You should have a script (if you don't want to have two different scripts),
which would return an image or a page depending on its parameters,
something like http://your.site.net/cgi-bin/witty_page.py?action=page to return
"""
...... Content-type: plain/text\n\n
......<img src="witty_page.py?action=image">
......
"""
and http://your.site.net/cgi-bin/witty_page.py?action=image to return a picture.

As an example, please have my counter script. It can return either a picture,
or a page: a 1x1 pix. picture when counting pages (...?site=999&actn=incr)
or an html page with the visitors list (...?site=999&actn=list).

Regards, Georgy

#!C:\Apps\Python\python.exe

import sys, os

def read_counter( file_name ):
  counter = "0"
  if os.access( file_name, os.R_OK|os.W_OK ):
    f = open( file_name, "rt" )
    if f:
      counter = f.read().strip()
      if counter == '': counter = "0"
      f.close()
  return counter

def write_counter( file_name, counter ):
  f = open( file_name, "wt" )
  if f:
    f.write( counter )
    f.close()

file_cnt = "counter/c_nn_%04d.dat" # i.e. in cgi-bin/counter/
file_ips = "counter/c_ip_%04d.dat"
file_dsc = "counter/c_descr.dat"

def show_list( site, name ):
  file_cnt_name = file_cnt % site
  file_ips_name = file_ips % site

  counter = read_counter( file_cnt_name )

  f = open( file_ips_name, "rt" )
  if not f:
    raise ValueError

  lines = f.readlines()
  f.close()

  style = "<style>td { padding: 1px 6px 1px 6px; text-align: left; }</style>"
  title = "<title>Visits to %s</title>" % name

  sys.stdout.write( "Content-type: text/html\n\n" ) # yield html

  sys.stdout.write( "<html><head>%s%s</head>" % (style,title) )
  sys.stdout.write( "<body>%s visits to <b>%s</b>"
                    "<p><table border=1 cellspacing=0>\n" % (counter,name) )
  sys.stdout.write( "<tr bgcolor='#C0FFFF'><td>%s</td><td>%s</td><td>%s</td></tr>\n"
                    % ('Date','Time','IP') )
  try:
    gray = False
    for ln in lines:
      ln = ln.strip()
      if not ln:
        continue
      dt,tm,ip = ln.split()
      if   ip == '217.77.77.77': ip = 'my friend 1'
      elif ip == '218.88.88.88': ip = 'my friend 2'
      if gray: bg = "#E0E0E0"
      else:    bg = "#FFFFFF"
      gray = not gray
      sys.stdout.write( "<tr bgcolor='%s'><td>%s</td><td>%s</td><td>%s</td></tr>\n"
                        % ( bg,dt,tm,ip ) )
  except Exception,e:
    sys.stdout.write( "<font color=red>%s</font>" % e )

  sys.stdout.write( "</body></html>" )
  sys.stdout.close()

def do_count( site ): # returns image

  file_cnt_name = file_cnt % site
  file_ips_name = file_ips % site

  counter = read_counter( file_cnt_name )

  counter = str(int(counter)+1)
  write_counter( file_cnt_name, counter )

  import datetime
  dt = str(datetime.datetime.now())[:-7]

  ip = os.environ.get("REMOTE_ADDR",'?')

  f = open( file_ips_name, "at" )
  if f:
    f.write( "%s %s\n" % (dt,ip) )
    f.close()

  return_image()

def return_image():
  # 1x1 black pixel
  data = ('GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00'
          '\xFF\xFF\xFF\x21\xF9\x04\x00\x00\x00\x00\x00'
          '\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x03\x44\x01\x00\x3B')

  sys.stdout.write( "Content-type: image/gif\n\n" )
  sys.stdout.write( data )
  sys.stdout.close()

def read_descriptions( filename ):
  dict = {}
  for line in file( filename ):
    if not line or line[0]=='#': # ignore empty and comment lines
      continue
    fields = line.split()
    if len(fields) < 2:
      continue
    num, title = fields[:2]
    dict[int(num)] = title
  return dict

sites = read_descriptions( file_dsc )

# now we accept:
# site=sitenumber (all digits)
# actn=list|incr

# How to insert a counter into your page:
# <img src="http://my.site.org/cgi-bin/counter.py?site=99&actn=incr"
#  border=0 width=1 height=1 alt=''>

def processing():
  try:
    if os.environ.has_key('QUERY_STRING'):
      query = os.environ['QUERY_STRING']
      import parsequery
      query = parsequery.parsequery( query ) # get dictionary
      if 'site' in query and 'actn' in query:
        site = query['site']
        actn = query['actn']
        site = int(site)
        if site in sites:
          if actn == 'list':
            show_list( site, sites[site] )
            return
          if actn == 'incr':
            do_count( site )
            return
  except:
    pass
  # all bad cases -- just return a picture
  return_image()

processing() # used to allow return from the middle

# EOF





More information about the Python-list mailing list