Python:pickle.load中的EOF错误

时间:2016-05-27 14:38:57

标签: python pickle

我正在学习教程并编写代码来使用pickle存储和检索cookie

import requests
import pickle
def save_cookies(cookies, filename):
        with open(filename, 'wb') as f:
                pickle.dump(cookies, f)

def load_cookies(filename):
        with open(filename, 'rb') as f:
                return pickle.load(f)


url = 'http://xxxxxxxxxxxxxx/here'
data = {'value': 50, 'username': 'shreyUsr', 'password': 'pass'}
r = requests.post(url, data, cookies=load_cookies('cookie_store.dat'))
save_cookies(r.cookies, 'cookie_store.dat')

print("CLIENT SIDE: Status Code is " + str(r.status_code) + "Reason is " + str(r.reason) )
print("SERVER SIDE: Response is " + str(r.text))

运行代码会出现以下错误:

File "./cookieComm.py", line 15, in <module>
    r = requests.post(url, data, cookies=load_cookies('cookie_store.dat'))
  File "./cookieComm.py", line 10, in load_cookies
    return pickle.load(f)
  File "/usr/lib/python2.7/pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 880, in load_eof
    raise EOFError
EOFError

请告知错误。

1 个答案:

答案 0 :(得分:0)

从它的外观来看,您的cookie_store.dat文件当前是空的,因为您在将Cookie保存到以下行之前尝试加载:

r = requests.post(url, data, cookies=load_cookies('cookie_store.dat'))
save_cookies(r.cookies, 'cookie_store.dat')