Possible bug on email module.

José Rui Faustino de Sousa jrfsousa.removeFF2934A6this at esoterica.pt
Thu May 8 16:06:27 EDT 2003


Hi!

This is a small script to do a CGI POST request the problem seams to be
with the MIME encoding done by the email module:

import sys as _sy
import os as _os
import httplib as _hl
import email.MIMEMultipart as _mm
import email.MIMEText as _mt

def post( server, link, headers, dmpf ):
    srv=_hl.HTTPConnection( server )
    srv.set_debuglevel( 7 )
    msg=_mm.MIMEMultipart(_subtype="form-data")
    for key, val in headers.items():
        prt=_mt.MIMEText(None)
        if isinstance(val,dict):
            prt.set_type("text/xml")
            fil = open( val["filename"], "r" )
            prt.set_payload(fil.read())
            fil.close()

prt.add_header("Content-Disposition","form-data",name=key,filename=val["
filename"])
        else:
            pld = val
            prt.add_header("Content-Disposition","form-data",name=key)
            prt.set_payload(val)
        msg.attach(prt)
    msg.epilogue=''
    hdr, epr = msg.as_string().split("\n\n",1)
    hdr={}
    for hnm, hvl in msg.items(): hdr[hnm]=hvl
    #
    #Here is the problem:
    #Comment out the following line and the POST request fails.
    #
    epr="\r\n".join(epr.split("\n"))
    #
    #It would seam that the email module is not formating properly the
MIME stuff.
    #
    srv.request("POST",link,epr,hdr)
    rsp=srv.getresponse()
    fil=open(dmpf,"w")
    fil.write(rsp.read())
    fil.close()
    srv.close()

if __name__ == "__main__":
    submit =  False
    filename = _sy.argv[1]
    dumpfile = _sy.argv[2]
    if _os.path.isfile( filename ):
        fext = _os.path.splitext( filename )[1]
        if fext.lower() in ( ".htm", ".html" ): submit = True
    if submit:
        hdr={"charset":"(detect
automatically)","doctype":"Inline","ss":"1","sp":"1","outline":"1","noat
t":"1","No200":"1","verbose":"1"}
        hdr["uploaded_file"]={"filename":filename}
        post( "validator.w3.org", "/check", hdr, dumpfile )
    else:
        print "Arg error..."

Best regards
José Rui


-- 
========================================================================
"[Of gene therapy] The chance of doing this will remain infinitely small
to the last syllable of recorded time". Macfarlane Burnet
========================================================================
iam://Jose Rui Faustino de Sousa http://homepage.esoterica.pt/~jrfsousa/
mailto://jrfsousa.removeFF2934A6this@esoterica.pt phone://+351-239444940
address://rua Carlos A. Pinto de Abreu 30C, 1 3040-245 Coimbra Portugal
========================================================================






More information about the Python-list mailing list