program tip

파이썬 요청 파일 업로드

radiobox 2020. 9. 9. 07:53
반응형

파이썬 요청 파일 업로드


Python 요청 라이브러리를 사용하여 파일을 업로드하는 간단한 작업을 수행하고 있습니다. Stack Overflow를 검색했는데 아무도 같은 문제가없는 것 같았습니다. 즉, 파일이 서버에서 수신되지 않는다는 것입니다.

import requests
url='http://nesssi.cacr.caltech.edu/cgi-bin/getmulticonedb_release2.cgi/post'
files={'files': open('file.txt','rb')}
values={'upload_file' : 'file.txt' , 'DB':'photcat' , 'OUT':'csv' , 'SHORT':'short'}
r=requests.post(url,files=files,data=values)

'upload_file'키워드의 값을 내 파일 이름으로 채 웁니다. 비워두면

Error - You must select a file to upload!

그리고 이제 나는

File  file.txt  of size    bytes is  uploaded successfully!
Query service results:  There were 0 lines.

파일이 비어있는 경우에만 나타납니다. 그래서 파일을 성공적으로 보내는 방법에 대해 고민했습니다. 이 웹 사이트로 이동하여 양식을 수동으로 채우면 일치하는 개체의 멋진 목록이 반환되기 때문에 파일이 작동한다는 것을 알고 있습니다. 모든 힌트에 감사드립니다.

관련된 다른 스레드 (그러나 내 문제에 대답하지 않음) :


경우 upload_file파일 사용으로 의미 :

files = {'upload_file': open('file.txt','rb')}
values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'}

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

requests과 여러 부분 폼 POST 본문을 보내드립니다 upload_file의 내용에 필드 설정 file.txt파일.

파일 이름은 특정 필드의 MIME 헤더에 포함됩니다.

>>> import requests
>>> open('file.txt', 'wb')  # create an empty demo file
<_io.BufferedWriter name='file.txt'>
>>> files = {'upload_file': open('file.txt', 'rb')}
>>> print(requests.Request('POST', 'http://example.com', files=files).prepare().body.decode('ascii'))
--c226ce13d09842658ffbd31e0563c6bd
Content-Disposition: form-data; name="upload_file"; filename="file.txt"


--c226ce13d09842658ffbd31e0563c6bd--

filename="file.txt"매개 변수에 유의하십시오 .

files더 많은 제어가 필요한 경우 2 ~ 4 개의 요소가 있는 매핑 값에 튜플을 사용할 수 있습니다 . 첫 번째 요소는 파일 이름, 그 뒤에 콘텐츠, 선택적 콘텐츠 유형 헤더 값 및 추가 헤더의 선택적 매핑입니다.

files = {'upload_file': ('foobar.txt', open('file.txt','rb'), 'text/x-spam')}

이것은 선택적 헤더를 제외하고 대체 파일 이름과 콘텐츠 유형을 설정합니다.

다른 필드가 지정되지 않은 파일에서 전체 POST 본문을 가져 오는 것을 의미하는 경우 files매개 변수를 사용하지 말고 파일을 data. 그런 다음 설정할 수 있습니다 Content-Type아무 것도 그렇지 않으면 설정되지 않습니다으로 너무 헤더를.


(2018) 새로운 파이썬 요청 라이브러리는이 프로세스를 단순화했습니다. 'files'변수를 사용하여 멀티 파트 인코딩 파일을 업로드하고 싶다는 신호를 보낼 수 있습니다.

url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}

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

클라이언트 Uplaod

Python requests라이브러리를 사용 하여 단일 파일을 업로드하려는 경우 requests lib 는 스트리밍 업로드를 지원하므로 메모리를 읽지 않고도 대용량 파일 이나 스트림 보낼 수 있습니다 .

with open('massive-body', 'rb') as f:
    requests.post('http://some.url/streamed', data=f)

서버 측

Then store the file on the server.py side such that save the stream into file without loading into the memory. Following is an example with using Flask file uploads.

@app.route("/upload", methods=['POST'])
def upload_file():
    from werkzeug.datastructures import FileStorage
    FileStorage(request.stream).save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
    return 'OK', 200

Or use werkzeug Form Data Parsing as mentioned in a fix for the issue of "large file uploads eating up memory" in order to avoid using memory inefficiently on large files upload (s.t. 22 GiB file in ~60 seconds. Memory usage is constant at about 13 MiB.).

@app.route("/upload", methods=['POST'])
def upload_file():
    def custom_stream_factory(total_content_length, filename, content_type, content_length=None):
        import tempfile
        tmpfile = tempfile.NamedTemporaryFile('wb+', prefix='flaskapp', suffix='.nc')
        app.logger.info("start receiving file ... filename => " + str(tmpfile.name))
        return tmpfile

    import werkzeug, flask
    stream, form, files = werkzeug.formparser.parse_form_data(flask.request.environ, stream_factory=custom_stream_factory)
    for fil in files.values():
        app.logger.info(" ".join(["saved form name", fil.name, "submitted as", fil.filename, "to temporary file", fil.stream.name]))
        # Do whatever with stored file at `fil.stream.name`
    return 'OK', 200

참고URL : https://stackoverflow.com/questions/22567306/python-requests-file-upload

반응형