User:Eleanorg/1.2/Forbidden Pixels/writing string to text file

From XPUB & Lens-Based wiki

This script recieves a url from a form, scrapes it, extracts relevant bits with regex, and writes relevant bits to a text file.

#!/usr/bin/python
#-*- coding:utf-8 -*-

import cgi, re, urllib2
import cgitb; cgitb.enable()

 
# scrapes pixel data string from a URL submitted by user in an html form; assigns the result to appropriate variables.
 

#------------- get URL from input form -------------------#

form = cgi.FieldStorage()		# Grabs whatever input comes from form
url = form.getvalue("url", "http://ox4.org/~nor/trials/hostedString.html")  # assigns form's input to var 'url'. url on the right is a default value that will be printed for testing if nothing is recieved from the form

#------------- scrape webpage----------------------------#

text = urllib2.urlopen(url).read()	# reads page at the specified URL

 
#--------- extract the string with regex; write it to text file------------#

# string is in format: 
# Pixel position:500.001; Color:rgba(222,221,217,1)
 

htmlHeader = """<!DOCTYPE html>
<html>
  <head>
    <title>A form talking to a python script</title>
    <style type="text/css">
    </style>
  </head>
  <body>"""

print "Content-Type: text/html"
print 
print htmlHeader

pat = r"(Pixel position:(\d\d\d).(\d\d\d)\;\ Color:(rgba\(.*\)))"

if not re.search(pat, text):			# if pattern 'pat' isn't found within text
    print "nothing found"
else:
    f = open("data/data.txt", 'a')  		# opens text file in apend mode
    for m in re.finditer(pat, text):		# for each match in text
        string = m.group(0)                     # m.group() lets you access data within the match object
	xPos = m.group(1)
	yPos = m.group(2)
	color = m.group(3)
	f.write(xPos + '\n')			# writes xPos to the text file plus newline
        print "thanks, url submitted. The pixel you are hosting has been added."
    f.close()
print """
   </body>
</html>"""