2016-11-07 14 views
0

サーバ/クライアント(requests lib)から画像ファイルをhttp-send(eg requests.post)し、これらのファイルをジャンゴ休憩フレームワークアプリ。これはどうすればいいですか?リクエスト/ django restフレームワークを使ってローカル画像ファイルを送信、受信、保存する

第2に、requests.postによって一般的に送信されるQueryDictから部品を抽出する方法を知りたいと思います。具体的には、このデータセットから_io-Objectを解析して保存するにはどうすればよいですか?

# sending app 

file = "path/to/image.jpg" 
data = open(file, 'rb') 

files = { 
    'json': (None, crawlingResultConnectionsJson, 'application/json'), 
    'file': (os.path.basename(file), open(file, 'rb'), 'application/octet-stream') 
} 

url = "http://127.0.0.1:8000/result/" # receiving/saving django rest framework app 

r = requests.post(url, files=files) 

私は今かなり時間をかけました。あなたの助けが大いに評価されるでしょう!ありがとう!

答えて

0

私のニーズに完全に合った解決策になった。私は送信側または受信側のいずれかの貢献だけを見つけたので、ここですべてをまとめようとします。

私のアプローチは、分離されたリクエストでjsonとイメージを転送することです。以下の2つのアプリは完全に独立しています。

(不要サーバとアプリ)以下のように送信側が行われます。

from django.core.serializers.json import DjangoJSONEncoder 
import requests # http://docs.python-requests.org/en/master/ 
import datetime # in case... 
import json 

### send image stuff ### 
urlImages = "http://127.0.0.1:8000/receive-images/" 
file = "C:\\path\\to\\filename.jpg" # "\\" windows machine... 

# this will probably run in a loop or so to process a bunch of images 
with open(file, 'rb') as f: 
    filename = "filename.jpg" 
    files = {'file': (filename, f)} 

    r = requests.post(urlImages, files=files) 

print(r) # some logging here 

### send data stuff ### 
data = data # python data - lists, dicts, whatever 
json = json.dumps(data, cls=DjangoJSONEncoder) # DjangoJSONEncoder handles datetime fields 
urlData = "http://127.0.0.1:8000/receive-data/" 
headers = {'content-type': 'application/json'} 

r = requests.post(urlData, json, headers=headers) 

print(r) # some logging here 

(ビルトインDjangoのサーバーDEV用のサーバーを実行する必要が受信側、WSGInterfaceとApacheを生産中)、それは、この親友がインストールされています:

from rest_framework.views import APIView 
from rest_framework.response import Response 
from rest_framework import status 
from .api_controller import ApiController 
from django.core.files.storage import default_storage 

class ReceiveImages(APIView): # make sure to nail down corresponding url-confs 
    def post(self, request, format=None): 

     file = request.data.get('file') 
     filename = str(file) 

     with default_storage.open('images/' + filename, 'wb+') as destination: 
      for chunk in file.chunks(): 
       destination.write(chunk) 

     return Response("ok", status=status.HTTP_200_OK) 

class ReceiveData(APIView): # make sure to nail down corresponding url-confs 
    def post(self, request, format=None): 
     json = request.data 

     ApiController().createDataIfNotExists(json) 
     # As my json is quite complex, 
     # I've sourced out the DB-interactions to a controller-like class (coming 
     # from PHP symfony :)) with heavy use of the great 
     # MyModel.objects.get_or_create() method. Also the strptime() method is used 
     # to convert the datetime fields. This could also go here... 

     return Response("ok", status=status.HTTP_200_OK) 
01: http://www.django-rest-framework.org/

は最後に、私たちは要求を処理するための2つのビューを持っています

同意するか、これを向上させることができることはないと思う場合https://stackoverflow.com/a/30195605/6522103

てください(!)コメント/解答に関してでチャンクを()を使用。ありがとう!!

関連する問題