![](/img/trans.png)
[英]How to receive and save an image in HTTP post request in Python Sockets?
[英]In python how to receive and save image in HTTP post request without using requests and other libraries that are not installed in python
我嘗試從python中的HTTP發布請求接收圖像。 我正在使用BaseHTTPHolder和do_POST()函數。 我嘗試在本地主機上運行服務器時從本地主機地址http:127.0.0.1:8080 / photo接收圖像。 我嘗試使用下面的代碼,它保存照片,但由於未完全收到照片而無法打開。
img = urllib2.urlopen('http://127.0.0.1:8080/photo').read()
content_length = int(self.headers.getheader('content-length',0))
file_content = self.rfile.read(content_length)
with open('/Users/kasymhan/Desktop/sprint2/file01.jpg','wb') as s:
s.write(file_content)
編輯我的do_POST()函數
def do_POST(self):
url = 'http://127.0.0.1:8080/photo/file02.jpg'
request_headers = self.headers
content_length = request_headers.getheaders('content-length')
length = int(content_length[0]) if content_length else 0
file_content = self.rfile.read(length)
img = urllib2.urlopen(url).read()
with open('/Users/kasymhan/Desktop/sprint2/image.jpg','wb') as s:
s.write(img)
為什么不直接將圖像的內容寫入輸出文件:
import urllib2
url = "http://farm5.static.flickr.com/4105/5063092779_17b2133825_b.jpg"
img = urllib2.urlopen(url).read()
with open('./image.jpg','wb') as s:
s.write(img)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.