<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 5.50.4916.2300" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Hi I try to upload a picture with a python cgi
script. Tried many versions of the code, this is the current. </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Here's the code and error!</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>The error:</FONT></DIV>
<DIV><FONT face=Arial size=2><FONT face=Arial size=2></FONT></FONT> </DIV>
<DIV><FONT face=Arial size=2><FONT face="Times New Roman" size=3>Traceback (most
recent call last): File "C:\cgi-bin\upload.py", line 179,<BR>in ? xmldokument =
skjema['dok'].value File "C:\Python22\lib\cgi.py", line<BR>550, in __getitem__
raise KeyError, key KeyError: dok </FONT></DIV>
<DIV><BR> </DIV>
<DIV> </DIV>
<DIV>The code:</DIV>
<DIV> </DIV>
<DIV>#!c:\python22\python.exe<BR># FileTransfer.py - CGI file transfer, may
require Python 1.5 or higher<BR># Author: <A
href="mailto:JeffBauer@bigfoot.com">JeffBauer@bigfoot.com</A><BR># Prior
art: Aaron Watters, Jim Fulton</DIV>
<DIV> </DIV>
<DIV>import xml.dom<BR>import xml.dom.minidom<BR>import xml.parsers</DIV>
<DIV> </DIV>
<DIV>import os, sys, traceback<BR>import httplib<BR>import tempfile<BR>from
whrandom import randint<BR>from string import joinfields<BR>from time import
time<BR>import cgi</DIV>
<DIV> </DIV>
<DIV>test = 0</DIV>
<DIV> </DIV>
<DIV>FileUploadRequestException="Dette gikk ikke :"</DIV>
<DIV> </DIV>
<DIV>def parseDok(xmlstreng): <BR>s=[]</DIV>
<DIV> </DIV>
<DIV>pars = xml.dom.minidom.parseString(xmlstreng)<BR>elementene =
pars.getElementsByTagName('element') </DIV>
<DIV> </DIV>
<DIV>for element in elementene:<BR>ss={}<BR>ss['dataid']
=<BR>element.getAttribute('dataid').encode('iso_8859_1')<BR>ss['type']
=<BR>element.getAttribute('type').encode('iso_8859_1')<BR>ss['format']
=<BR>element.getAttribute('format').encode('iso_8859_1')</DIV>
<DIV> </DIV>
<DIV>s += [ss]</DIV>
<DIV> </DIV>
<DIV>return s</DIV>
<DIV> </DIV>
<DIV>def lagFilblokk(elementliste,xmldokument):<BR>#path = "<A
href="http://192.168.0.32/upload/">http://192.168.0.32/upload/</A>"<BR>path =
"<A href="http://192.168.0.32/upload/">http://192.168.0.32/upload/</A>"<BR>#path
= "O:\\"<BR>#Ting skal vist hete userfile og attach1 o.s.v.<BR>s =
[{'fieldname':'userfile','filename':xmldokument}]<BR>att = 0<BR>for element in
elementliste:<BR>att += 1<BR>ss={}<BR>ss['fieldname'] = "attach%s" %
att<BR>ss['filename'] = path + element['dataid']
+<BR>filending(element['type'],element['format'])</DIV>
<DIV> </DIV>
<DIV>s += [ss]</DIV>
<DIV> </DIV>
<DIV>return s</DIV>
<DIV> </DIV>
<DIV>def filending(type,format):<BR>#Dette skal gjøres bedre<BR>if format ==
'jpeg':<BR>return '.jpg'<BR>if format == 'jpg':<BR>return '.jpg'<BR>return
'.txt'</DIV>
<DIV> </DIV>
<DIV><BR>class FileUploadRequest:<BR>
"""<BR> File upload modul, og sender en liste filer, i med
gitte feltnavn.<BR> """<BR> def
__init__(self, uri, host, port):<BR>
self.uri = uri<BR> self.host =
host<BR> self.port =
port<BR> self.queryString =
''<BR>self.teller = 1<BR>
#self.boundary= '%s%s_%s' % ('-tr--', int(time()),
randint(1,10000))<BR>self.boundary= '%s%s_%s' % ('rrrr', time(),
randint(1,10000))</DIV>
<DIV> </DIV>
<DIV>#print self.boundary</DIV>
<DIV> </DIV>
<DIV><BR> def load(self, filenames, headerDict=None):<BR>for
filename in filenames:<BR>if not '?' in filename['filename']:<BR>#Dette er ikke
et xml dokument (som jo begynner
med<BR><?xml...<BR> f =
open(filename['filename'],'rb')<BR>
data = f.read(1000000)<BR> #Problem
her kanskje<BR>
f.close()<BR>else:<BR>#Det er ikke noe filnavn dataene<BR>data =
filename['filename']<BR>filename['filename'] =
'test.xml'<BR> hdr = []; part = [];
total = []<BR>
hdr.append('Content-Disposition: form-data; name="%s";<BR>filename="%s"' %
(filename['fieldname'],filename['filename']))<BR>
hdr.append('Content-Type:
application/octet-stream')<BR>
hdr.append('Content-Length: %d' %
len(data))<BR> # Add user-defined
header values here, if provided.<BR> #
Reminder: FieldStorage will convert key to
lowercase.<BR> if type(headerDict) ==
type({}):<BR>
for k in
headerDict.keys():<BR>
hdr.append("%s: %s", (k,
headerDict[k]))<BR>
part.append("%s\n\n%s" % (joinfields(hdr,'\n'),
data))<BR> total.append('--%s\n' %
self.boundary)<BR>
total.append(joinfields(part, "\n--%s\n" %
self.boundary))<BR>
#total.append('\n--%s--\n' % self.boundary)<BR>#print joinfields(total,
'')<BR> self.queryString +=
joinfields(total, '')<BR>self.teller +=1<BR>self.queryString +=('\n--%s--\n' %
self.boundary) #Dette er<BR>sluttmarkeringen</DIV>
<DIV> </DIV>
<DIV> def
request(self):<BR> query =
self.queryString<BR> contentType =
'multipart/form-data; boundary=%s' %
self.boundary<BR> contentLength =
str(len(query))<BR> h =
httplib.HTTP()<BR>
h.connect(self.host, self.port)<BR>
h.putrequest('POST', self.uri)<BR>
h.putheader('Accept', '*/*')<BR>
h.putheader('Proxy-Connection',
'Keep-Alive')<BR>
h.putheader('User-Agent', 'Glue0.1 [no] (Mac OSX;
U)')<BR> h.putheader('Content-Type',
contentType)<BR>
h.putheader('Content-Length',
contentLength)<BR>
h.endheaders()<BR>
h.send(query)<BR> rcode, rmsg, headers
= h.getreply()<BR> response =
h.getfile().read(10000)<BR>"""<BR> if
rcode !=
200:<BR> msg =
"error: %s, %s\n%s %s" % (rcode, self.uri, rmsg,
response)<BR>
raise
FileUploadRequestException(msg)<BR>"""<BR>f=file('c:\\RESPONSE.html','w')<BR>f.write(response)<BR>f.close<BR>##<BR>f=file('c:\\qvery.txt','w')<BR>f.write(query)<BR>f.close<BR>##<BR>
return response<BR> return
response</DIV>
<DIV> </DIV>
<DIV>def lastoppListe(host,uri,filenames):<BR>
<BR> <BR> port = 9091<BR>
port = 8080<BR> #port = 80<BR>
<BR> fileUploadRequest = FileUploadRequest(uri, host,
port)<BR> <BR>
fileUploadRequest.load(filenames)<BR> response =
fileUploadRequest.request()<BR> return response</DIV>
<DIV> </DIV>
<DIV>def lesFil(filNavn):<BR>r = open(filNavn)<BR>dok =
r.read(19000)<BR>r.close()<BR>return dok</DIV>
<DIV> </DIV>
<DIV>def main(self,xmldokument):<BR>#Først finne hvilke elementer vi har å jobbe
med<BR>elementliste = parseDok(xmldokument) <BR>filliste =
lagFilblokk(elementliste,xmldokument)<BR>#Denne returnerer en liste over
dataposter<BR>#print filliste</DIV>
<DIV> </DIV>
<DIV>svar = lastoppListe('192.168.0.1','/files/upload',filliste)<BR>print
svar</DIV>
<DIV> </DIV>
<DIV><BR>if __name__ == '__main__':<BR>print "Content-type:
text/html"<BR>print<BR>if test:<BR>xmldokument =
lesFil('test.xml')<BR>else:</DIV>
<DIV> </DIV>
<DIV>skjema = cgi.FieldStorage()<BR>xmldokument = skjema['dok'].value</DIV>
<DIV> </DIV>
<DIV>alfa= main(None, xmldokument)<BR></DIV>
<DIV> </DIV>
<DIV>Regards stan</DIV></FONT></BODY></HTML>