วันพุธที่ 10 กุมภาพันธ์ พ.ศ. 2553

Turbogears2 How to receive Data on MIME

ในส่วนนี้ เกิดขึ้นจากปัญหาการรับ request data ที่ได้จากข้อมูลสองชุด ลักษณะข้อมูลที่ได้จาก body


#Example data 1
#-------------------------------------------------

--sendReport@sdgs:8I50nM01
Content-Type: text/xml; charset=utf-8
Content-Transfer-Encoding: binary
Content-ID:

...data xml format string...
--sendReport@sdgs:8I50nM01
Content-Type: application/x-gzip
Content-Transfer-Encoding: binary
Content-ID: 3884032_2010020817_sdgs.log.gz
Content-Location: 3884032_2010020817_sdgs.log.gz
Content-Description: "attachment"; filename="3884032_2010020817_sdgs.log.gz"; size=180;

...data binary string...
--sendReport@sdgs:8I50nM01--

#-------------------------------------------------
#โดยที่
#data ชุดแรกเป็น xml string ธรรมดา
#data ชุดที่สองเป็น zip file ที่ถูกส่งมาในรูปแบบ binary


#Example data 2
#-------------------------------------------------

------=_Part_0_201002091513
Content-type: text/xml;charset="UTF-8"
Content-Length: 1840

...data xml format string...
------=_Part_0_201002091513
Content-Type: text/plain; name=sms-text.txt; charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: sms-text.txt

...data string...
------=_Part_0_201002091513--

#-------------------------------------------------
#โดยที่
#data ชุดแรกเป็น xml string ธรรมดา
#data ชุดที่สองเป็น text string ธรรมดา

เราจัดการรแยกข้อมูลออกเป็นสองชุดโดยการ

def get_data(self, data):
    printlog.debug('get_data_mo_receive : %s' %data)
    if None == data or '' == data.strip():
        rreceivee Exception('invalid request from receive.')

    '''
        get --xxxyy:zzz 
    '''
    ds = data.split()[0]
    '''
        delete --xxxyy:zzz-- end of message
    '''
    data2 = data.replace('%s--' %ds, '')
    printlog.debug(' read line partition : %s ', ds)
    dats = data2.split(ds)
    xml = ''
    req_msg = ''
    content_type = 'text'

    dats = [d.strip() for d in dats if d != None and '' != d.strip()]
    printlog.debug(' read line all data : %s ', dats)

    for dat in dats:
        try:
            printlog.debug(' read line data : %s ', dat)
        except:
            pass

        d = email.message_from_string(dat)
        try:
            printlog.debug(' read line content_type : %s ', d.get_content_type())
        except:
            pass

        try:
            printlog.debug(' read line payload : %s ', d.get_payload())
        except:
            pass

        '''
            type of content
            text/xml : main data
            text/plain : request mo 
            application/x-gzip : offline process
        '''
        if 'text/xml' == d.get_content_type():
            xml = d.get_payload()

        elif 'application/x-gzip' == d.get_content_type():
            content_type = 'file'
            content_location = d.get('Content-ID').strip()
            f = open('/usr/local/appprod/upload/receive/offline/receive/%s' %content_location, 'wb')
            f.write(d.get_payload())
            f.close()

        elif 'text/plain'== d.get_content_type():
            content_type = 'text'
            req_msg = d.get_payload().strip()

    return xml.strip(), req_msg.strip(), content_type

#-------------------------------------------------

ในที่นี้ จะทำการ return xml ออกมา พร้อม request แต่
ในกรณี file จะทำการ save ลง path แล้วเพื่อเอาไปทำงานอย่างอื่นต่อไป
เนื่องจาก file ที่ได้รับอาจมีหลากหลายประเภท

ไม่มีความคิดเห็น: