I need help please. this cgi program works fine only if the user puts
in more than 66 chars..i chopped the lines into 66 chars each so they would fit in the textarea the problem is that record comments() works perfectly for writing multiple lines and the \n char in commentlog.txt but for a single line i use recordcomment() and no matter how i tried it it wouldnt write the extra \n line....as u can see i desperately need that extra line that is a \n so parseposts() can put the posts in their correct orders. thanks for your help.
#!c:/Python26/python.exe
import os
import cgi
def getpostcount():
f=open('C:/commentlog.txt', 'r')
a=f.readlines()
f.close()
i=0
for line in a:
if line.startswith( "\n" ):
i+=1
return i
def clearold():
f=open('C:/commentlog.txt', 'r')
a=f.readlines()
f.close()
for line in a:
a=a[1:]
if line=="\n":
break
f=open('C:/commentlog.txt', 'w')
for line in a:
f.write(line)
f.close()
def oneline():
try:
form=cgi.FieldStorage()
q=form.getvalue("posts")
if len(q)>66:
return 1
except:
return 0
def parseposts(posts):
b=[]
c=[]
for a in posts:
if not a=="\n":
a=a[:-1]
b.append(a)
else:
c.append(b)
b=[]
return c
def getposts():
f=open('C:/commentlog.txt', 'r')
a=f.readlines()
f.close()
b=parseposts(a)
return b
def postit():
a=getposts()
print "Content-type: text/html\r\n\r\n"
print "<style type=\"text/css\">"
print "h2 {"
print "color : green;"
print "}"
print "textarea.input {"
print " color : black;"
print " background-color : #00f000;"
print "}"
print "body {"
print " background-color : black; } "
print "</style>"
print "<html>"
print "<head>"
print "<img src=\" \" width=\"240\" height=\"230\" align=\"right\">"
print "</head>"
print "<body>"
print "<h2>This is my second page anyone can post comments anonymously<br>"
print "<form action=\"comments.py\" method=\"POST\">"
print "<textarea class=\"input\"name=\"posts\" rows=\"10\" cols=\"65\">"
print "</textarea>"
print "<input type=\"submit\" value=\"post\">"
print "</form>"
print "<hr>"
print "</body>"
print "</html>"
for post in a:
print "<textarea readonly class=\"input\" rows=\"10\" cols=\"65\">"
for line in post:
print line
print "</textarea>"
print "<hr>"
def parsepost(post):
a=[]
b1=post
while len(b1)>66:
b2=b1[:65]
b1=b1[65:]
b2+="\n"
a.append(b2)
b1+="\n"
a.append(b1)
return a
def getpost():
try:
form=cgi.FieldStorage()
q=form.getvalue("posts")
s=q
if len(s)>66:
s=parsepost(q)
return s
except:
return 0
def recordcomment(post):
f=open('C:/commentlog.txt', 'a')
f.write(post)
f.write("\n")
f.close()
def recordcomments(posts):
f=open('C:/commentlog.txt', 'a')
for a in posts:
f.write(a)
f.write("\n")
f.close()
def main():
comment=getpost()
o=oneline()
if comment:
count=getpostcount()
if count>=20:
clearold()
if o==None:
comment+="\n"
recordcomment(comment)
else:
recordcomments(comment)
try:
main()
postit()
except:
print "Content-type: text/html\r\n\r\n"
print"err"