Python:Mechanize无法将cookie发送到第二个URL

Python:Mechanize无法将cookie发送到第二个URL,python,cookies,mechanize,Python,Cookies,Mechanize,当请求第二个URL时,我在尝试让Mechanize保留第一个请求的URL设置的cookie时遇到了严重问题。为了测试是否发送了任何内容,我在服务器上放置了以下文件(cookies.php): 以下是输出: <mechanize._clientcookie.CookieJar[Cookie(version=0, name='NID', value='50=rkj1MMbufL7KRMj00TMF4rI4x7VNYgzWk5P97V05gBAMVOrYuSbb6-hpXVC3y_eD999uE

当请求第二个URL时,我在尝试让Mechanize保留第一个请求的URL设置的cookie时遇到了严重问题。为了测试是否发送了任何内容,我在服务器上放置了以下文件(cookies.php):

以下是输出:

<mechanize._clientcookie.CookieJar[Cookie(version=0, name='NID', value='50=rkj1MMbufL7KRMj00TMF4rI4x7VNYgzWk5P97V05gBAMVOrYuSbb6-hpXVC3y_eD999uECgnBn7YqZ-ZGB1kmWhc_wQWV9nKlPER4_3BWEVSGU632vXEhgYROAz3QrP5', port=None, port_specified=False, domain='.google.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1331337059, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), Cookie(version=0, name='PREF', value='ID=20342e7c6a6b8f8b:FF=0:TM=1315525859:LM=1315525859:S=RppxtfAGwVsGkZiJ', port=None, port_specified=False, domain='.google.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1378597859, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>
http://[MY DOMAIN].com/cookies.php
Date: Thu, 08 Sep 2011 23:51:01 GMT
Server: Apache mod_fcgid/2.3.6 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/5.2.17
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

cookies:::Array
(
)

http://[MY DOMAIN].com/cookies.php
日期:2011年9月8日星期四23:51:01 GMT
服务器:Apache mod_fcgid/2.3.6 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By:PHP/5.2.17
连接:关闭
传输编码:分块
内容类型:text/html
cookies:::数组
(
)

你有什么办法让它工作吗?应该注意的是,上面的代码是从文档中复制粘贴的…,我还尝试了文档提供的其他示例代码,可以在这里找到:

request1
request2
来自不同的域。可以理解的是,来自
google.com
的cookie没有发送到
'[domain2].com'

非常感谢-我会尝试一下,然后马上回来。
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
import mechanize

from pprint import *

#

class NullCookieProcessor(mechanize.HTTPCookieProcessor):
    def http_request(self, request): return request
    def http_response(self, request, response): return response

opener = mechanize.build_opener(NullCookieProcessor)

request = mechanize.Request('http://www.google.com')
response = mechanize.urlopen(request)
cj = mechanize.CookieJar()
cj.extract_cookies(response, request)

pprint(cj)

request2 = mechanize.Request('http://[domain2].com/cookies.php')
cj.add_cookie_header(request2)
response2 = mechanize.urlopen(request2)

print response2.geturl()
print response2.info()  # headers
print response2.read()  # body (readline and readlines work too)
<mechanize._clientcookie.CookieJar[Cookie(version=0, name='NID', value='50=rkj1MMbufL7KRMj00TMF4rI4x7VNYgzWk5P97V05gBAMVOrYuSbb6-hpXVC3y_eD999uECgnBn7YqZ-ZGB1kmWhc_wQWV9nKlPER4_3BWEVSGU632vXEhgYROAz3QrP5', port=None, port_specified=False, domain='.google.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1331337059, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), Cookie(version=0, name='PREF', value='ID=20342e7c6a6b8f8b:FF=0:TM=1315525859:LM=1315525859:S=RppxtfAGwVsGkZiJ', port=None, port_specified=False, domain='.google.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1378597859, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>
http://[MY DOMAIN].com/cookies.php
Date: Thu, 08 Sep 2011 23:51:01 GMT
Server: Apache mod_fcgid/2.3.6 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/5.2.17
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

cookies:::Array
(
)