Technique/Python

[python] requests 첨부파일 및 데이터 전송 예제

_투덜이스머프 2020. 12. 19. 22:57
반응형

requests 사용하여 첨부 파일 전송

 

import requests
    file = {
        'narray': convert_bytes,
        'x_coord': x_coord.tobytes(),
        'y_coord': y_coord.tobytes(),
    }
    data = {
        'shape1': shape1,
        'shape2': shape2,
        'shape3': shape3,
        'dtype': roi_array.dtype,
        'x_shape': x_coord_shape,
        'x_dtype': x_coord.dtype,
        'y_shape': y_coord_shape,
        'y_dtype': y_coord.dtype,
    }

    response = requests.post('http://xxx.xxx.xxx.xxx/detection', files=file, data=data)

 

간단한 코드입니다.

python 코드에서 파일을 post로 전송하는 예제입니다.

file ={ } 부분은 전송하려는 파일을 byte로 읽어서 dict로 만들어 전송합니다.

data = {} 부분은 첨부 파일 이외에 데이터들을 보낼 수 있도록 data를 만들어 데이터를 담았습니다.

 

requests.post로 파일을 전송하는데 전송 파라미터는 Url, 첨부파일, 데이터 형식으로 전송을 합니다.

 

반응형