第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

寫入 NamedTemporaryFile 靜默失敗;將 Curl cookie jar 轉(zhuǎn)換為

寫入 NamedTemporaryFile 靜默失敗;將 Curl cookie jar 轉(zhuǎn)換為

紅顏莎娜 2023-04-18 16:37:18
我正在嘗試獲取 Curl 吐出的 Netscape HTTP Cookie 文件并將其轉(zhuǎn)換為 Requests 庫可以使用的 Cookiejar。我netscapeCookieString在我的 Python 腳本中有一個變量,它看起來像:# Netscape HTTP Cookie File# https://curl.haxx.se/docs/http-cookies.html# This file was generated by libcurl! Edit at your own risk..miami.edu  TRUE    /   TRUE    0   PS_LASTSITE https://canelink.miami.edu/psc/PUMI2J/由于我不想自己解析 cookie 文件,所以我想使用cookielib. 遺憾的是,這意味著我必須寫入磁盤,因為cookielib.MozillaCookieJar()不會將字符串作為輸入:它必須采用文件。所以我正在使用NamedTemporaryFile(無法開始SpooledTemporaryFile工作;如果可能的話,再次想在內(nèi)存中完成所有這些操作)。tempCookieFile = tempfile.NamedTemporaryFile()# now take the contents of the cookie string and put it into this in memory file# that cookielib will read from. There are a couple quirks though. for line in netscapeCookieString.splitlines():    # cookielib doesn't know how to handle httpOnly cookies correctly    # so we have to do some pre-processing to make sure they make it into    # the cookielib. Basically just removing the httpOnly prefix which is honestly    # an abuse of the RFC in the first place. note: httpOnly actually refers to    # cookies that javascript can't access, as in only http protocol can    # access them, it has nothing to do with http vs https. it's purely     # to protect against XSS a bit better. These cookies may actually end up    # being the most critical of all cookies in a given set.    # https://stackoverflow.com/a/53384267/2611730    if line.startswith("#HttpOnly_"):        # this is actually how the curl library removes the httpOnly, by doing length        line = line[len("#HttpOnly_"):]    tempCookieFile.write(line)tempCookieFile.flush()但問題是,這是行不通的!print tempCookieFile.read()打印一個空行。因此,pprint.pprint(cookieJar)打印一個空的餅干罐。我怎樣才能真正寫信給 a NamedTemporaryFile?
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻(xiàn)1863條經(jīng)驗 獲得超2個贊

寫入文件后,指向該文件的指針指向?qū)懭霐?shù)據(jù)之后的位置(在您的文件末尾),因此當(dāng)您讀取它時返回一個空字符串(文件末尾后沒有更多數(shù)據(jù))只是尋找 0讀之前


>>> import tempfile

>>> tempCookieFile = tempfile.NamedTemporaryFile()

>>> tempCookieFile.write("hey")

>>> tempCookieFile.seek(0)

>>> print(tempCookieFile.read())


查看完整回答
反對 回復(fù) 2023-04-18
  • 1 回答
  • 0 關(guān)注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號