Wednesday, May 30, 2007

How to download a web page using IronPython

Downloading a web page is a common programming task. Here are two snippets of code that show how to do it with IronPython. The code was tested on Windows and Mac (with Mono).

Using the WebClient class

from System.Net import WebClient
dataStream = WebClient().OpenRead('http://google.com')
from System.IO import StreamReader
reader = StreamReader(dataStream)
result = reader.ReadToEnd()
print result

Using the WebRequest and WebResponse classes

from System.Net import WebRequest
request = WebRequest.Create("http://google.com")
response = request.GetResponse()
responseStream = response.GetResponseStream()
from System.IO import StreamReader
result = StreamReader(responseStream).ReadToEnd()
print result

24 comments:

Evan said...

Seems rather more complex than:

import urllib
content = urllib.urlopen("http://google.com").read()
print content

I think the above code should work on any python implementation. Not sure why you are using the .NET libraries (unless IronPython doesn't have urllib?).

Andrzej Krzywda said...

Evan,

Unfortunately, the code you pasted doesn't work with IronPython. Urllib2 is also not supported in the official release of IronPython (lack of the md5 module).

Fuzzyman said...

I think MD5 is now a built in in IronPython.

The problem is more that urllib2 uses some 'undocumented' attributes of sockets that weren't implemented in IronPython.

This will be fixed, but in the meantime using the .NET classes is easy enough.

Evan said...

Ah, I haven't been following IronPython that closely. Good info to remember if I try it in the future.

I noticed both of you mentioned urllib2, but I wonder if urllib (no 2) might work since I think its simpler than urllib2.

Fuzzyman said...

No - I'm pretty sure that because of problems with sockets that both urllib2 and urllib are broken in IronPython.

There is a fix in FePy, so it might be worth trying that. The problems are recorded as IronPython bugs on Codeplex and so they will get fixed eventually.

In the meantime, using the .NET classes is not quite so nice - but works fine.

Anonymous said...

I think the moral of the post is to just stick with CPython.

Andrzej Krzywda said...

Anonymous,

Yes, if you don't need .Net then it's better to stick with CPython.

Dave said...

I'm a little late to the discussion but the .NET code can be simplified as:

content = WebClient().DownloadString("http://google.com")

BuyLevitra said...

Thanks for article!

Phentermine said...

Thanks for interesting article.

Viagra said...

Glad to read articles like this. Thanks to author!

Anonimous said...

Excellent website. Good work. Very useful. I will bookmark!

plush said...

companies marketing mineral makeups and also get the best bargains in mineral makeup you can imagine,
find aout how to consolidate your students loans or just how to lower your actual rates.,
looking for breast enlargements? in Rochester,
homeopathy for eczema learn about it.,
Allergies, information about lipitor,
save big with great bargains in mineral makeup,

change edition interviewing motivational people preparing second
,

interviewing motivational people preparing second time
,

interviewing people motivational preparing for a second time
,

black mold exposure
,

black mold exposure symptoms
,

black mold symptoms of exposure
,

free job interview questions
,

free job interview answers
,

interview answers to get a job
,

lookfor hair styles for fine thin hair
,

search hair styles for fine thin hair
,

hair styles for fine thin hair
,

beach resort in the philippines
,

great beach resort in the philippines
,

luxury beach resort in the philippines
,
iron garden gates, here,
iron garden gates,
wrought iron garden gates
, here
,
wrought iron garden gates
,
You: The Owner's Manual: An Insider's Guide to the Body That Will Make You Healthier and Younger
,
eat eating mindless more than think we we why
,


texturizer,
texturizers here,
black hair texturizer,
find aout how care curly hair,
find about how to care curly hair,
care curly hair,
lipitor rash,
lipitor reactions,
new house ventura california,
the house new houston tx,
new house washington dc,
new house pa philadelphia,
san antonio tx house new,
house new pa philadelphia,
new house washington dc,
new house ventura california,
the house new houston tx,
house new san antonio tx,
the house new houston tx, that you are looking for,
new house ventura california, you need to buy,
new house washington dc,
house new pa philadelphia,
new house san antonio tx,

hair surgery transplant
,

air filter allergy
,

refurbished dell laptop computers
,

hair surgery transplant
,

air filter allergy
,

refurbished dell laptop computers
,

hair surgery transplant
,

air filter allergy
,

refurbished dell laptop computers
,

chocolate esophagus heartburn study
,

chocolate esophagus heartburn study
be informed,

digestion healing healthy heartburn natural preventing way
,

digestion healing healthy heartburn natural preventing way
,
sew skirts, 16simple styles you can make!,
sew what skirts 16 simple styles you,
rebates and discounts on sunsetter awnings,
sunsetter awnings discounts and rebates,
discount on sunsetter awnings


truck and bus tires 12r 22.5, get the best price,
tires truck and bus 12r 22.5 best price,
tires truck bus tires12r 22.5 best price,
plush car seat strap covers,
car seat strap covers,plush,
car seat strap, plush covers,
oscoda voip phone systems, the best!,
oscoda voip the phone system,
oscoda voip phone systems,
exterior iron gates,
oriental wrought iron gates,
powder coated iron garden fencing,

iphone said...

black mold exposure,
black mold symptoms of exposure,

wrought iron garden gates,
your nest iron garden gates, here,

hair styles for fine thin hair,
search hair styles for fine thin hair,

night vision binoculars,
buy, night vision binoculars,

lipitor reactions,
lipitor reactions,


luxury beach resort in the philippines,
beach resort in the philippines,

homeopathy for baby eczema.,
homeopathy for baby eczema.,

save big with great mineral makeup bargains,
companies marketing mineral makeups,

prodam iphone praha,
Apple prodam iphone praha,

iphone clone cect manual,
manual for iphone clone cect,

fero 52 binoculars night vision,
fero 52 night vision,

best night vision binoculars,
buy, best night vision binoculars,

computer programs to make photo albums,
computer programs, make photo albums,

Medications from Europe said...

Order prescription medication from Europe for weight loss, cholesterol, asthma, antibiotics, pain relief, heartburn, anxiety, sexual performace, blood pressure treatment.


Order Generic Viagra Sildenafil drug is used to treat impotence in men. Sildenafil increases the bodys ability to achieve and maintain an erection during sexual stimulation.

Brand Kamagra drug and brand Penegra are a top selling generic versions of Viagra , world renowned for its unbeatable potency and efficiency.

Generic Cialis TADALAFIL drug, dubbed "Le Weekend Drug" in Europe, is a new treatment for Erectile Dysfunction (ED) from Lilly ICOS LLC. Clinical studies show that Brand Cialis works faster, lasts up to 36 hours and has less side effects than Viagra.

Generic Levitra VARDENAFIL drug is one of the latest treatments for Erectile Dysfunction. Levitra acts in the same way as Viagra by blocking an enzyme called PDE-5. Clinical studies show that Brand Levitra works faster and has less side effects than Viagra.

Generic Trial packs are your way to evaluate different brand ED drugs and decide which is best for you before ordering the larger packs.

said...

婚約指輪
成長ホルモン
広島 不動産
障害者
松山市 不動産
結婚相談所 東京
国際協力
結婚指輪

said...

浮気調査
募金
ゼネラリ
群馬 ハウスメーカー
埼玉 ハウスメーカー
盲導犬
治験
出産祝い
クレジットカード決済
24そんぽ24
アメリカンホームダイレクト
出会い
出会い系
出会い系サイト

said...

出会いサイト
自動車保険
自動車保険 比較
お見合いパーティー
チューリッヒ
自動車 保険 見積
不動産
ソニー損保
カード決済
インプラント
ショッピングカート
東京 ホームページ制作
不動産投資
アスクル
埼玉 不動産

said...

三井ダイレクト
カラーコンタクト
カーボンオフセット
コンタクトレンズ
知多半島 温泉
知多半島 旅館
プリンセスルーム
アクサダイレクト
賃貸
不動産
不動産投資
岡山 不動産
网络营销
輸入雑貨
セルライト

Anonymous said...

EH株式会社
エクセルヒューマン
EH株式会社
エクセルヒューマン
EH株式会社
エクセルヒューマン
EH株式会社
エクセルヒューマン
EH株式会社
エクセルヒューマン
EH株式会社
エクセルヒューマン
EH株式会社
エクセルヒューマン
株式会社
エクセルヒューマン
エクセルヒューマン
EH株式会社
エクセルヒューマン
EH株式会社

Anonymous said...

クレジットカード現金化
賃貸 大田区
賃貸 北区
賃貸 江東区
賃貸 品川
賃貸 渋谷
賃貸 新宿
賃貸 杉並
賃貸 世田谷
賃貸 中央区
賃貸 千代田区
賃貸 池袋
賃貸 中野
賃貸 文京区
賃貸 港区
賃貸 目黒
賃貸 新築
賃貸 ペット可
賃貸 楽器可
賃貸 手数料なし
賃貸 保証人不要
賃貸 駅5分以内
賃貸 部屋探し
東京 部屋探し
デザイナーズ 賃貸
賃貸 分譲仕様
賃貸 中央線
賃貸 京浜東北線
賃貸 京王線
賃貸 東横線
賃貸 丸ノ内線

fds said...

物流网是现代物流产品设备资讯传媒. 水工业网面向给排水领域设计院所、-电源自来水厂、污水处理厂及市政管理部门,面向工业污水处理、工业制水、水文水利、楼宇供水及水泵应用等水工业领域用户,发布和交流各种传感器、检测分析仪表、SCADA设备、人机界面监控系统及调速装置的产品、-机械传动
-机器视觉
-传感器
-现场仪表
-显示控制仪表技术、应用、解决方案及市场信息;探讨、推进我国水工业自动化技术、节能技术应用发展。视频,多媒体,自动化,工控视频,自动化视频, PLC教程,变频器教程,软件教程,自动化行业视频新媒体的创造者和领先者-工控TV,教程,播客, PLC,可编程序控制器,自动化软件。同时产品频道有DCS -PAC- PC-BASED-CPCI- PXI-嵌入式系统-
SCADA

-自动化软件
-工业以太网
-现场总线
-无线通讯
-低压变频器
-高压变频器
-运动控制
-分析测试仪表
-执行机构
-工业安全
-低压电器

fds said...

shanghai hotel-guangzhou hotel- shengzhen hotel- beijing hotel Resorts with good discount and accept online reservation. 3000 hotels and resorts with up to 75% discount off published rates for your preview and online reservation. guangzhou hotel Shenzhen hotel shanghai hotel beijing hotel

徐州回转支承 公司提供转盘轴承 --slewing ring slewing bearing slewing bearings服务.

Anonymous said...

情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,按摩棒,震動按摩棒,微調按摩棒,情趣按摩棒,逼真按摩棒,G點,跳蛋,跳蛋,跳蛋,性感內衣,飛機杯,充氣娃娃,情趣娃娃,角色扮演,性感睡衣,SM,潤滑液,威而柔,香水,精油,芳香精油,自慰套,自慰,性感吊帶襪,吊帶襪,情趣用品加盟AIO交友愛情館,情人歡愉用品,美女視訊,情色交友,視訊交友,辣妹視訊,美女交友,嘟嘟成人網,成人網站,A片,A片下載,免費A片,免費A片下載愛情公寓,情色,舊情人,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,一葉情貼圖片區,情色小說,色情,色情遊戲,情色視訊,情色電影,aio交友愛情館,色情a片,一夜情,辣妹視訊,視訊聊天室,免費視訊聊天,免費視訊,視訊,視訊美女,美女視訊,視訊交友,視訊聊天,免費視訊聊天室,情人視訊網,影音視訊聊天室,視訊交友90739,成人影片,成人交友,美女交友,微風成人,嘟嘟成人網,成人貼圖,成人電影,A片,豆豆聊天室,聊天室,UT聊天室,尋夢園聊天室,男同志聊天室,UT男同志聊天室,聊天室尋夢園,080聊天室,080苗栗人聊天室,6K聊天室,女同志聊天室,小高聊天室,上班族聊天室,080中部人聊天室,同志聊天室,聊天室交友,中部人聊天室,成人聊天室,一夜情聊天室,情色聊天室,寄情築園小遊戲情境坊歡愉用品,情趣用品,成人網站,情人節禮物,情人節,AIO交友愛情館,情色,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,七夕情人節,色情,情色電影,色情網站,辣妹視訊,視訊聊天室,情色視訊,免費視訊聊天,美女視訊,視訊美女,美女交友,美女,情色交友,成人交友,自拍,本土自拍,情人視訊網,視訊交友90739,生日禮物,情色論壇,正妹牆,免費A片下載,AV女優,成人影片,色情A片,成人論壇,情趣,免費成人影片,成人電影,成人影城,愛情公寓,成人影片,保險套,舊情人,微風成人,成人,成人遊戲,成人光碟,色情遊戲,跳蛋,按摩棒,一夜情,男同志聊天室,肛交,口交,性交,援交,免費視訊交友,視訊交友,一葉情貼圖片區,性愛,視訊,視訊聊天,A片,A片下載,免費A片,嘟嘟成人網,寄情築園小遊戲,女同志聊天室,免費視訊聊天室,一夜情聊天室,聊天室