473,807 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python equivalent of LWP and HTTP in Perl

I have a script that I've written in Perl that retrieves files generated
from a template. It works decently enough, but I'd like to rewrite it in
Python (Perl was just a detour; it was originally Sed).

Was wondering what the closest thing to what I'm using now in Perl
(LWP::UserAgent and HTTP::Request:C ommon) is in Python. The main bit of
my code is fairly simple.

my($ua) = LWP::UserAgent->new;
my $req = GET "$uri";
$req->header(Refer er => "$referer") ;
$ua->proxy('http' , 'http://localhost:8080/'); # Proxomitron
$response = $ua->request($req );
$respcode = $response->code;

Jul 18 '05 #1
4 8455
Dekaritae wrote:
I have a script that I've written in Perl that retrieves files generated
from a template. It works decently enough, but I'd like to rewrite it in
Python (Perl was just a detour; it was originally Sed). my($ua) = LWP::UserAgent->new;
my $req = GET "$uri";
$req->header(Refer er => "$referer") ;
$ua->proxy('http' , 'http://localhost:8080/'); # Proxomitron
$response = $ua->request($req );
$respcode = $response->code;

import urllib
proxies = {'http': 'http://localhost:8080/'}
response = urllib.urlopen( "some_url", proxies=proxies ).read()

--Irmen
Jul 18 '05 #2

"Irmen de Jong" wrote
my($ua) = LWP::UserAgent->new;
my $req = GET "$uri";
$req->header(Refer er => "$referer") ;
$ua->proxy('http' , 'http://localhost:8080/'); # Proxomitron
$response = $ua->request($req );
$respcode = $response->code;

import urllib
proxies = {'http': 'http://localhost:8080/'}
response = urllib.urlopen( "some_url", proxies=proxies ).read()


.... which is short and clean, but ignores the Referer header.
I have worked with httplib some, but not urllib. Maybe urllib can do this,
but may I suggest checking out httplib:
http://docs.python.org/lib/httplib-examples.html

-ej
Jul 18 '05 #3
"Erik Johnson" <sp**@nospam.or g> writes:
"Irmen de Jong" wrote
> my($ua) = LWP::UserAgent->new;
> my $req = GET "$uri";
> $req->header(Refer er => "$referer") ;
> $ua->proxy('http' , 'http://localhost:8080/'); # Proxomitron
> $response = $ua->request($req );
> $respcode = $response->code;

import urllib
proxies = {'http': 'http://localhost:8080/'}
response = urllib.urlopen( "some_url", proxies=proxies ).read()


... which is short and clean, but ignores the Referer header.
I have worked with httplib some, but not urllib. Maybe urllib can do this,
but may I suggest checking out httplib:
http://docs.python.org/lib/httplib-examples.html


Try urllib2:

import urllib2

proxy_support = urllib2.ProxyHa ndler({"http" : "http://localhost:8080/"})
opener = urllib2.build_o pener(proxy_sup port)
urllib2.install _opener(opener)

req = urllib2.Request ("some_url")
req.add_header( "Referer", referer)
response = urllib2.urlopen (req)
There's fancier handlers too, if you need digest authentication, for
example. If you have the environment variable 'http_proxy' set to your
proxy, you don't need to do the install_opener jazz.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)phy sics(dot)mcmast er(dot)ca
Jul 18 '05 #4
Dekaritae <ma**@mdi.ca> wrote in message news:<vA******* ******@fe61.use netserver.com>. ..
I have a script that I've written in Perl that retrieves files generated
from a template. It works decently enough, but I'd like to rewrite it in
Python (Perl was just a detour; it was originally Sed).

Was wondering what the closest thing to what I'm using now in Perl
(LWP::UserAgent and HTTP::Request:C ommon) is in Python. The main bit of
my code is fairly simple.

my($ua) = LWP::UserAgent->new;
my $req = GET "$uri";
$req->header(Refer er => "$referer") ;
$ua->proxy('http' , 'http://localhost:8080/'); # Proxomitron
$response = $ua->request($req );
$respcode = $response->code;


urllib2 is definitely the way forward for adding proxy support and
request headers. Very easy. If you want cookie handling you'll need to
look at ClientCookie which does similar things to the perl library you
mention.

Regards,

Fuzzy
http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
6588
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
699
34292
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
226
12736
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
1
2747
by: Edward WIJAYA | last post by:
Hi, I am new to Python, and I like to learn more about it. Since I am used to Perl before, I would like to know what is Python equivalent of Perl code below: $filename = $ARGV;
41
3564
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes.
68
5906
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
20
4088
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
0
2268
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of images: /Users/t/t4/oh/DSCN2059m-s.jpg
14
2096
by: ccdetail | last post by:
http://www.tiobe.com/index.htm?tiobe_index Python is the 7th most commonly used language, up from 8th. The only one gaining ground besides VB in the top 10. We're glad, our app is written in python. It's free at http://pnk.com and it is a web timesheet for project accounting
10
1415
by: Mladen Gogala | last post by:
I am a Python newbie who decided to see what that Python fuss is all about. Quite frankly, I am a bit perplexed. After having had few months of experience with Perl (started in 1994 with Perl v4, and doing it ever since) , here is what perplexes me: perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;' The equivalent in Python looks like this: Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51)
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5547
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.