Python:从Web读取大文本文件而不将其加载到内存中

时间:2016-07-14 19:25:21

标签: python

我正在寻找从网络服务器读取大文本文件的最佳方式。

我当前的脚本如下所示:

from requests.auth import HTTPBasicAuth

import requests 

def log_in(url, login_name, login_pwd):
    res = requests.get(url, auth=HTTPBasicAuth(login_name, login_pwd))

    return res # return back the res-object

if __name__ == "__main__":

    # my example link: http://www.sophus.bplaced.net/test/xar.txt
    while True:
        url = unicode(raw_input("Enter URL: "))
        login_name = unicode(raw_input("Enter your nickname: "))
        login_pwd = unicode(raw_input("Enter your password: "))

        result = log_in(url, login_name, login_pwd)

        if result.ok:
            print "Your login was successful"
            print result.text
            break
        else:
            print "Your login is not validated"

这个脚本确实有效,但在我目前的代码中,我担心文本文件的完整内容被加载到内存中。或者我错了?通常,当我从本地机器读取文件时,我总是使用生成器来完成。但在这种情况下,我想从网络服务器上读一个(可能很大的?)文本文件,所以我不能用生成器来做,对吗?

进行测试:用户名:xar和用户密码:456123

修改 我不想下载文本文件,我只想阅读它。

更新 嘿伙计们,谢谢你的帮助。我想,我在你的帮助下找到了解决方案。

from requests.auth import HTTPBasicAuth
from contextlib import closing

import requests

def log_in(url, login_name, login_pwd):
    res = requests.get(url, auth=HTTPBasicAuth(login_name, login_pwd), stream=True)
    return res

if __name__ == "__main__":

    # my example link: http://www.sophus.bplaced.net/test/xar.txt
    # For testing:  username: xar
    #               password 456123
    while True:
        url = unicode(raw_input("Enter URL: "))
        login_name = unicode(raw_input("Enter your nickname: "))
        login_pwd = unicode(raw_input("Enter your password: "))

        result = log_in(url, login_name, login_pwd)

        if result.ok:
            with closing(result) as my_file:

                for chunk in result.iter_content(chunk_size=1024, decode_unicode=True):
                    print "text", chunk

            break

        else:
            print "Your login is not validated"
你怎么看?有更好的方法还是有问题?

0 个答案:

没有答案