2016-11-24 61 views
1

我的挑战是将我的cookie与我的有效载荷一起发送。我只是将一个cookie添加到现有的和正在运行的程序中。有效载荷(歌曲)到达那里就好了。而且,我在浏览器控制台没有收到任何错误。浏览器没有得到我的python服务器cookie?

我似乎能够发送一个cooke,以及我的JSON数据就好了。

当我查看Chrome开发人员工具中的标题时,我看到了cookie。但是,当我在应用程序>> Cookie选项卡中查找cookie时,没有任何内容。所以,cookie头似乎被发送,客户端并没有存储它。

而且,在客户端.... document.cookie为空。

我的测试代码正在创建一个名为sessionID的cookie。

这里是响应头在Chrome中:你可以看到我的假会话ID的Cookie:

Access-Control-Allow-Credentials:true 

Access-Control-Allow-Origin:null 

Content-Type:text/plain 

Date:Wed, 23 Nov 2016 22:41:10 GMT 

Server:BaseHTTP/0.6 Python/3.5.2 

Set-Cookie:sessionID=4jjdd7ghhq 

这里是指示不再饼干截图。 no cookies in application

这里是我的服务器代码,写在Python3:

def do_OPTIONS(self): 
    self.send_response(200)  
    self.send_header("Access-Control-Allow-Origin", self.headers["Origin"]) 
    self.send_header("Access-Control-Allow-Credentials", "true")    
    self.send_header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') 
    self.send_header('Access-Control-Allow-Headers', 'Content-Type') 
    self.end_headers() 
    return 

def do_GET(self): 
    if self.path.startswith("/songs/ID"): 
     self.load_cookie() 
     db=MediaDB() 
     ID = self.parseID(self.path) 
     song = db.getSong(ID) 
     self.send_response(200) 
     self.send_header('Access-Control-Allow-Origin', self.headers["Origin"]) 
     self.send_header("Access-Control-Allow-Credentials", "true") 
     self.send_header("Content-Type", "text/plain")  
     self.cookie["sessionID"] = "4jjdd7ghhq" 
     self.send_cookie() 
     self.end_headers() 
     json_string = json.dumps(song) 
     self.wfile.write(bytes(json_string, "utf-8")) 
    elif self.path.startswith("/songs"): 
     self.getSongs() 
     return 
    else: 
     self.badPath() 
     return 


def load_cookie(self): 
    if "Cookie" in self.headers: 
     self.cookie = cookies.SimpleCookie(self.headers["Cookie"]) 
    else: 
     self.cookie = cookies.SimpleCookie() 
    return 

def send_cookie(self): 
    for morsel in self.cookie.values(): 
     self.send_header("Set-Cookie", morsel.OutputString()) 
    return 

================ 

我的搜索想出短。如果有什么东西可以帮助我,我感谢你指出了这一点。哦,顺便说一句,我的皮肤非常厚,所以如果我做了一些愚蠢的事情 - 可以指出来......我总得以某种方式学习。哈!

回答

0

默认情况下,当使用本地文件处理Chrome时,Chrome不支持cookie,正如我在您发布的屏幕截图中看到的那样。

但是,您可以通过使用--enable-file-cookies标志启动Chrome来更改此设置。

查看更多about in this post

+0

我的Chrome能够支持本地cookie,我有一个cookie测试,在没有添加JSON负载的情况下工作得很好。但是,我使用了上面的-enable-file-cookies,但仍然不适合我。谢谢。 – Iggy

相关问题