8 Star 0 Fork 8

src-openEuler/python-requests-ftp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
PR28-02-Adding-code-3-to-retr4ieve-status_code.patch 2.47 KB
一键复制 编辑 原始数据 按行查看 历史
朱春意 提交于 2019-11-06 19:50 . update code
diff -purN requests-ftp-0.3.1.orig/requests_ftp/ftp.py requests-ftp-0.3.1/requests_ftp/ftp.py
--- requests-ftp-0.3.1.orig/requests_ftp/ftp.py 2018-08-02 10:13:15.801731787 -0400
+++ requests-ftp-0.3.1/requests_ftp/ftp.py 2018-08-02 10:14:20.633933095 -0400
@@ -9,6 +9,7 @@ from io import BytesIO
import cgi
import os
import socket
+import logging
from requests.exceptions import ConnectionError, ConnectTimeout, ReadTimeout
from requests.exceptions import RequestException
@@ -102,6 +103,35 @@ def build_binary_response(request, data,
return build_response(request, data, code, None)
+def get_status_code_from_code_response(code):
+ '''
+ The idea is to handle complicated code response (even multi lines).
+ We get the status code in two ways:
+ - extracting the code from the last valid line in the response
+ - getting it from the 3first digits in the code
+ After a comparaison between the two values,
+ we can safely set the code or raise a warning.
+
+ Examples:
+ - get_code('200 Welcome') == 200
+
+ - multi_line_code = '226-File successfully transferred\n226 0.000 seconds'
+ get_code(multi_line_code) == 226
+
+ - multi_line_with_code_conflits = '200-File successfully transferred\n226 0.000 seconds'
+ get_code(multi_line_with_code_conflits) == 226
+ '''
+ last_valid_line_from_code = [line for line in code.split('\n') if line][-1]
+ status_code_from_last_line = int(last_valid_line_from_code.split()[0])
+ status_code_from_first_digits = int(code[:3])
+ if status_code_from_last_line != status_code_from_first_digits:
+ logging.warning(
+ 'Status code seems to be non consistant.\n'
+ 'Code received: %d, extracted: %d and %d' % (
+ code, status_code_from_last_line, status_code_from_first_digits))
+ return status_code_from_last_line
+
+
def build_response(request, data, code, encoding):
'''Builds a response object from the data returned by ftplib, using the
specified encoding.'''
@@ -113,8 +143,8 @@ def build_response(request, data, code,
response.raw = data
response.url = request.url
response.request = request
- last_valid_line_from_code = [line for line in code.split('\n') if line][-1]
- response.status_code = int(last_valid_line_from_code.split()[0])
+ response.status_code = get_status_code_from_code_response(code)
+
if hasattr(data, "content_len"):
response.headers['Content-Length'] = str(data.content_len)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/src-openeuler/python-requests-ftp.git
git@gitee.com:src-openeuler/python-requests-ftp.git
src-openeuler
python-requests-ftp
python-requests-ftp
master

搜索帮助