2013-12-21 58 views
0

我想安全的圖像文件到服務器文件夾。 我通過cgi.Fieldstorage重新獲取圖像。該ADRESS和Base64編碼字符串看起來是這樣的:Base64解碼和寫入文件在Python發送Base64字符串通過cgi.Fieldstorage

http://localhost/upload.py?data={"base64" : "/9j/4AAQSkZJRgABAQEAYABgAAD/4QEMRXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAFgABkoYABwAAAOAAAAAkVU5JQ09ERQAASQBtAGEAZwBlACAAbABpAGMAZQBuAHMAZQBkACAAdABvACAAWgBhAHoAegBsAGUAIABJAG4AYwAuACAAQQBsAGwAIAB1AG4AYQB1AHQAaABvAHIAaQB6AGUAZAAgAHUAcwBlACAAaQBzACAAcAByAG8AaABpAGIAaQB0AGUAZAAuACAAZABlADAANgA0ADgAYgA0AC0ANwA4AGYAYgAtADQANQBkADIALQA4ADcANgBiAC0AZABkADYAOQA4ADgAYQAwADcAMgA4ADAAAAAAAAAAAAAAAAD/2wBDAAQDAwQDAwQEAwQFBAQFBgoHBgYGBg0JCggKDw0QEA8NDw4RExgUERIXEg4PFRwVFxkZGxsbEBQdHx0aHxgaGxr/2wBDAQQFBQYFBgwHBwwaEQ8RGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhr/wAARCAAyADIDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAg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hSxeHr/AMOaf5/cfH4jL8Xhf41Npd7affsddZf8edv/ANcl/lRSWBzY23/XJP5Ciuo885ezum0qW/gu7O+LNezyqYrOSVSruWUhlBHQ/hXkV/8AA7QL/wAet4jlu/ER02XVV1ifRW0p2t5LwAfPkpnaSoJXnPPrX0bRSaT3Gm1qjyu78F+CL0kz+EJMnqU0iZP/AEFRXG+M/gb4G8T6M1np+iXuk3QmWZLj+yJ7hcjgq0bDDKQenHOD2r6GornWFw6lzKCv6I7Hj8XKDg6srPpd2/M8H0D4QfD/AEHS9PtJfDeoXk9raRW8lw+n3QM2xcbioGBnk47Zrcbw14PsbaX+x/Cs8N6sbeQ40ifcHwcYYrxXrlFCwuHi+ZU1f0Q5Y/GSjySqya7cz/zK1jHJFY20ci4dIlVh7gCirNFdJwhRRRQAUUUUAFFFFABRRRQB/9k="} 

至於我發現Python使用標準「的Base64」編碼RFC 3548或RFC 4648,所以該字符串應該是正常的。

我嘗試此代碼:

def getParam(): 
    import json 
    import cgi 
    form3 = cgi.FieldStorage() 
    form2 = form3["data"].value 
    form1 =json.loads(form2) 
    return(form1) 

a = getParam() 
import base64 
print(a["base64"]) 
imgdata = base64.b64decode(a["base64"]) 
print("----------------------------------------------") 
print(imgdata) 
filename = '/var/www/images/some_image.jpg' 
t = open(filename, "wb") 
t.write(base64.b64decode(a["base64"])) 
t.close() 
print(filename) 

如果我執行是

/9j/4AAQSkZJRgABAQEAYABgAAD/4QEMRXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAFgABkoYABwAAAOAAAAAkVU5JQ09ERQAASQBtAGEAZwBlACAAbABpAGMAZQBuAHMAZQBkACAAdABvACAAWgBhAHoAegBsAGUAIABJAG4AYwAuACAAQQBsAGwAIAB1AG4AYQB1AHQAaABvAHIAaQB6AGUAZAAgAHUAcwBlACAAaQBzACAAcAByAG8AaABpAGIAaQB0AGUAZAAuACAAZABlADAANgA0ADgAYgA0AC0ANwA4AGYAYgAtADQANQBkADIALQA4ADcANgBiAC0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uX/s3Vv hkvv8AwHtv/jdV9QttYsrG5uE8RXjtDEzhWt7fBIGcHEftXSYnYUVHbyGW3ikb7zoGP1IooA8Y134vaN4C1y20rVHw9/q9wszHgQxtIwEhPTAYDIOODntTPi5rPgvW7RdH8SeNrPQIbWdXuleQAMf4VYkgDkisjxX8FIviN4oOo6jevHpsF9dedAjEOxEjYC9gM47E9eeAK O9fvrWHWNbsfE8M012moNvjktyY0mi cQ7iNpbKk7T64xXzePxFem3TqU aEn57K19v1PZ4fwUsZVryrVFDltyapXbT730uuivqfROkePF8L Jp7uGWPVtFt/NhggSQNH5LbV3KRkNlI48duPevpjwvPpd1odpN4eihg0 Vd6RwqFVCeSMDgHJOfevzdvNJ11tHsbfw9eQ6Lc20iOCowuF/hIGQRnmvZvh5 0Fr3gbTY9P16Oz1iMSN5kxX7OxKqoZuPlHI6Ae/UmvJyzMqWHcvazum3ZWu0fT43h3HVkuWnZpLdpJ/ifalUNb/wCQNqH/AF7Sf gmvK/C37Sfg3xCkIu5J9LmkjWQLMmV2t0O4dvriu m8T6PrmiXzaRqdpeBreTAimDH7p7V9hSxeHr/AMOaf5/cfH4jL8Xhf41Npd7affsddZf8edv/ANcl/lRSWBzY23/XJP5Ciuo885ezum0qW/gu7O LNezyqYrOSVSruWUhlBHQ/hXkV/8AA7QL/wAet4jlu/ER02XVV1ifRW0p2t5LwAfPkpnaSoJXnPPrX0bRSaT3Gm1qjyu78F CL0kz EJMnqU0iZP/AEFRXG M/gb4G8T6M1np iXuk3QmWZLj yJ7hcjgq0bDDKQenHOD2r6GornWFw6lzKCv6I7Hj8XKDg6srPpd2/M8H0D4QfD/AEHS9PtJfDeoXk9raRW8lw n3QM2xcbioGBnk47Zrcbw14PsbaX x/Cs8N6sbeQ40ifcHwcYYrxXrlFCwuHi ZU1f0Q5Y/GSjySqya7cz/zK1jHJFY20ci4dIlVh7gCirNFdJwhRRRQAUUUUAFFFFABRRRQB/9k= 
---------------------------------------------- 
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00\xff\xe1\x01\x0cExif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x01\x87i\x00\x04\x00\x00\x00\x01\x00\x00\x00\x16\x00\x01\x92\x86\x00\x07\x00\x00\x00\xe0\x00\x00\x00$UNICODE\x00\x00I\x00m\x00a\x00g\x00e\x00 \x00l\x00i\x00c\x00e\x00n\x00s\x00e\x00d\x00 \x00t\x00o\x00 \x00Z\x00a\x00z\x00z\x00l\x00e\x00 \x00I\x00n\x00c\x00.\x00 \x00A\x00l\x00l\x00 \x00u\x00n\x00a\x00u\x00t\x00h\x00o\x00r\x00i\x00z\x00e\x00d\x00 \x00u\x00s\x00e\x00 \x00i\x00s\x00 \x00p\x00r\x00o\x00h\x00i\x00b\x00i\x00t\x00e\x00d\x00.\x00 \x00d\x00e\x000\x006\x004\x008\x00b\x004\x00-\x007\x008\x00f\x00b\x00-\x004\x005\x00d\x002\x00-\x008\x007\x006\x00b\x00-\x00d\x00d\x006\x009\x008\x008\x00a\x000\x007\x002\x008\x000\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xdb\x00C\x00\x04\x03\x03\x04\x03\x03\x04\x04\x03\x04\x05\x04\x04\x05\x06\n\x07\x06\x06\x06\x06\r\t\n\x08\n\x0f\r\x10\x10\x0f\r\x0f\x0e\x11\x13\x18\x14\x11\x12\x17\x12\x0e\x0f\x15\x1c\x15\x17\x19\x19\x1b\x1b\x1b\x10\x14\x1d\x1f\x1d\x1a\x1f\x18\x1a\x1b\x1a\xff\xdb\x00C\x01\x04\x05\x05\x06\x05\x06\x0c\x07\x07\x0c\x1a\x11\x0f\x11\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\xff\xc0\x00\x11\x08\x002\x002\x03\x01"\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\x07"q\x142\x81\x91\xa1\x08#B\xb1\xc1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&\'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe1\xe2\xe19y\xb9\xfa:z\xbc|\xbc\xfd=}\xbd\xcf\x9f\xaf\[email protected]\x01\xf0\x10\x000\x10\x10\x10\x10\x10\x10\x10\x10\x10\x00\x00\x00\x00\x00\x00\x10 [email protected]`p\x80\x90\xa0\xbf\[email protected]\x0bQ\x10\x00 \x10 @@[email protected]@@\x00\x10\'p\x00\x10 1\[email protected]\x13\x10a$\x15\x10v\x17\x112#(\x10\x81D)\x1a\x1b\x1c\x10\x9235/\x01V\'-\x10\xa1bCN\x12_\x11q\x81\x91\xa2br\x82\x92\xa3Scs\x83\x93\xa44DTdt\x84\x94\xa55EUeu\x85\x95\xa66FVfv\x86\x96\xa77GWgw\x87\x97\xa8(8HXhx\x88\x98\xa9)9IYiy\x89\x99\xaa*:JZjz\x8a\x9a\xab+;K[k{\x8b\x9b\xac,<L\\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\xdeK\xc0\x07\xcf\x92\x99\xdaJ\x82W\x9c\xf3\xeb_F\xd1I\xa4\xf7\x1amj\x8f+\xbb\xf0P\x8b\xd2L\xc4$\xc9\xeaSH\x99?\xf0\x04\x15\x15\xc63\xf8\x1b\xe0o\x13\xe8\xcdg\xa6%\xee\x93t&Y\x92\xe3\xc8\x9e\xe1r8*\xd1\xb0\xc3)\x07\xa7\x1c\xe0\xf6\xaf\xa1\xa8\xaeu\x85\xc3\xa9s(+\xfa#\xb1\xe3\xf1r\x83\x83\xab+>\x97v\xfc\xcf\x07\xd0>\x10|?\xc0\x10t\xbd>\xd2_\r\xea\x17\x93\xda\xdaEo%\xc2}\xd03l\\n*\x06\x06y8\xed\x9a\xdco\rx>\xc6\xda_\x1f\xc2\xb3\xc3z\xb1\xb7\x90\xe3H\x9fp|\x1ca\x8a\xf1^\xb9E\x0b\x0b\x87\x89\x955\x7fD9c\xf1\x92\x8f$\xaa\xc9\xae\xdc\xcf\xfc\xca\xd61\xc9\x15\x8d\xb4r.\x1d"Ua\xee\x00\xa2\xac\xd1]\'\x08QE\x14\x00QE\x14\x00QE\x14\x00QE\x14\x01\xff\xd9' 
/var/www/images/some_image.jpg 

問題是我不能打開該文件產生的這種方式(不支持的標記類型0x68)中的響應。爲什麼?

編輯:我剛剛認識到,字符串被加載後+被替換

回答

2

如果你交出通過地址欄中的數據,+將自動轉換爲一個空的空間。只要string.replace('','+')在Base64-String上,你應該沒問題。