保存されるクッキーの形式は、「関連する記事」の、PHP でテストした cURL が保存する
形式とほぼ同じでした
▼ Python3.1
------------------------------------------------------------------------------------
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file! Do not edit.
localhost.local FALSE /web/test FALSE data 1234
localhost.local FALSE /web/test FALSE lightbox winofsql
▼ PHP の cURL
------------------------------------------------------------------------------------
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
localhost FALSE /web/test/ FALSE 0 data 1234
localhost FALSE /web/test/ FALSE 0 lightbox winofsql
------------------------------------------------------------------------------------
urllib.request.build_opener には、複数のハンドラを設定できます。
必要に応じて追加していく形になります( BASIC 認証や プロキシ等 )
HTTP ヘッダーの追加は、マニュアルにこっそり書かれていますが、
User-Agent: Python-urllib/3.1 は常にセットされるらしく、追加は
append メソッドで行います
関連する記事
VBScript/PHP クッキーデータが自動的に受け渡しされる事のテスト
VB.net : HttpWebRequest と HttpWebResponse でクッキーのやり取り
Python3.1 : mixi ボイスへ投稿
0004.py ( utf8n )
#! /usr/bin/env python3.1
import urllib.request
import http.cookiejar
# **********************************************************
# Cookie handling for HTTP clients
# python312.chm::/library/http.cookiejar.html
# **********************************************************
cj = http.cookiejar.MozillaCookieJar()
opener = urllib.request.build_opener(
urllib.request.HTTPCookieProcessor( cj )
)
# **********************************************************
# 最初の呼び出し
# ※ cookiejar を設定した opener で読みだす
# **********************************************************
try:
response = opener.open("http://localhost/web/test/sv1.php")
except urllib.error.URLError as e:
print(e)
exit()
html = response.read()
# **********************************************************
# サーバーからの http ヘッダ
# **********************************************************
print( response.info() )
# **********************************************************
# 内容
# **********************************************************
print( html.decode() )
# **********************************************************
# 2回目の呼び出し
# ※ cookiejar を設定した opener で読みだす
# ※ ヘッダーの追加は、opener.addheaders
# **********************************************************
opener = urllib.request.build_opener(
urllib.request.HTTPCookieProcessor( cj )
)
try:
opener.addheaders.append(('test-header', 'Python3.1'))
response = opener.open("http://localhost/web/test/sv2.php")
except urllib.error.URLError as e:
print(e)
exit()
html = response.read()
# **********************************************************
# サーバーからの http ヘッダ
# **********************************************************
print( response.info() )
# **********************************************************
# 内容
# **********************************************************
print( html.decode() )
# **********************************************************
# クッキーを保存
# ※ cj.load( "ファイルのパス" ) が可能です
# **********************************************************
cj.save( "cookie_100620.sav", ignore_discard=True, ignore_expires=True )
|
|
【Pythonの最新記事】
- Python : Excel の新しいブックのデフォルトのシートのセルに直接値をセットして、オートフィルを Range オブジェクトから実行する
- Python : shift_jis の3列の csv フォーマットのデータを ttk.Treeview に表示する
- Python : ttk.Treeview で表形式を使用して環境変数の一覧表示
- XAMPP + Python( 3.8 ) で WEBアプリの基礎部分構築 : その ( 1 )
- XAMPP + Python( 3.8 ) で WEBアプリの基礎部分構築 : その ( 4 ) : リダイレクトと関数とログ出力
- XAMPP + Python( 3.8 ) で WEBアプリの基礎部分構築 : その ( 3 ) : cgi.FieldStorage() から ディクショナリ
- XAMPP + Python( 3.8 ) でWEBアプリの基礎部分構築 : その ( 2 ) : QUERY_STRING と 画面定義
- Python + MySQL + IFRAME + Bootstrap : 問い合せ WEB アプリテンプレート
- MySQL Connector/Python の使用方法概要のまとめ
- Python3 : 言語的デザインの特徴と要点
- Python ドキュメントに沿った、テキストファイル読み込みの理解
- Python3 でメール送信 ( さくらインターネット )
- Python 3.6 で GET/POST メソッドを想定した CGI 用の簡易テンプレートを作成してみました
- Eclipse + Python(Pydev) : pywin32(COM使用の為) + MySQL Connector/ODBC でループ処理をしながら更新
- Eclipse + Python(Pydev) : MySQL Connector/Python でループ処理をしながら更新
- Pleiades Eclipse 4.7 Oxygen で Python を使って MySQL にアクセスする
- Python 3.4 : インストールと最初にする事( Windows )
- Python3.x : URL を読み出す( bytes から string )
- Python3.1 : URLを読み出して、バイナリのままファイル出力する
- Python3.1 : URLを読み出してファイルに出力






