我有一台服务器会生成一个文件,我想在文件准备好后将该文件发送到另一台服务器。 因此接收文件的服务器应始终侦听 并且我使用了Django rest框架,有人帮助我吗?
答案 0 :(得分:0)
服务器A(服务器从服务器B接收文件)
models.py
class TestModel(models.Model):
# Other fields you are interested in saving
file_data = models.FileField()
serializers.py
class TestModelSerializer(serializers.ModelSerializer):
class Meta:
model = TestModel
fields = '__all__'
view.py
class TestModelViewSet(viewsets.ModelViewSet):
queryset = TestModel.objects.all()
parser_classes = (MultiPartParser, FormParser,)
serializer_class = TestModelSerializer
urls.py
router = routers.DefaultRouter()
router.register(r'test', views.TestModelViewSet)
urlpatters = [
url(r'^api/', include(router.urls))
]
服务器B(192.168.5.5)(将文件发送到服务器A)
file_generator.py
with open('file.txt', 'rb') as f:
r = requests.post('http://192.168.5.5/api/test', files={'file.txt': f})