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; 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
"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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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}
|
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;
|
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.
| |
by: Lad |
last post by:
Is anyone capable of providing Python advantages over PHP if there are
any?
Cheers,
L.
|
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=;
|
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
|
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
|
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)
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |