473,394 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Building browser-like GET request

Hello

I'd like to download pages from a site, but it checks whether
the requests are coming from a live user or a script; If the latter,
the server returns a blank page.

Using a proxy (Paros), I can see what information my script and
FireFox send, and there are a lot of information that Python is
missing:

======== PYTHON ===============
http://www.acme.com/cgi-bin/read?code=123 HTTP/1.1
Accept-Encoding: identity
Host: www.acme.com
Connection: close
User-Agent: Python-urllib/2.4 Paros/3.2.12
======== FIREFOX ===============
http://www.acme.com/cgi-bin/read?code=123 HTTP/1.1
Host: www.acme.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3 Paros/3.2.12
Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: fr-fr,en-us;q=0.7,en;q=0.3
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
=============================

How can Python be told to send the same information?

Thank you.
Apr 21 '07 #1
4 4642
On 21 Apr., 23:28, Gilles Ganault <nos...@nospam.comwrote:
I'd like to download pages from a site, but it checks whether
the requests are coming from a live user or a script; If the latter,
the server returns a blank page.

Using a proxy (Paros), I can see what information my script and
FireFox send, and there are a lot of information that Python is
missing:
Well, I am brand new to Python, so it takes me a lot of guessing, but
since it seems you're using urlib2:

On http://docs.python.org/lib/module-urllib2.html is written that you
may add custom headers to your http requests.
Either by calling "addheader()" or by passing a dictionary with
headers to the constructor.

I hope that helped and I wasn't telling things you already new.
As a sidenote: For the task you describe I'd rather use an actual
sniffer - such as Wireshark (http://en.wikipedia.org/wiki/Wireshark),
than logs of a Proxy... Not sure wether Wireshark works under Windows,
though.

Good luck!

Apr 21 '07 #2
On 21 Apr 2007 14:47:55 -0700, Björn Keil <ab*****@silberdrache.net>
wrote:
>Well, I am brand new to Python, so it takes me a lot of guessing, but
since it seems you're using urlib2:
Thanks. Indeed, it looks like urlib2 is the way to go when going
through a proxy.

For those interested, here's how to download a page through a proxy:

----------------------------
import sys
import urllib
import urllib2
import re

#set up proxy
proxy_info = { 'host' : 'localhost','port' : 8080}
proxy_support = urllib2.ProxyHandler({"http" :
"http://%(host)s:%(port)d" % proxy_info})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)

#call page with specific headers
url = 'http://www.acme.com/cgi-bin/read?code=123'
headers = {
'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows
NT)',
'Accept' :
'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
'Accept-Language' : 'fr-fr,en-us;q=0.7,en;q=0.3',
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
}
#None = GET; set values to use POST
req = urllib2.Request(url, None, headers)

response = urllib2.urlopen(req).read()
log = open('output.html','w')
log.write(response)
log.close()
----------------------------

Thanks.
Apr 21 '07 #3
Björn Keil wrote:
[...]
>
I hope that helped and I wasn't telling things you already new.
As a sidenote: For the task you describe I'd rather use an actual
sniffer - such as Wireshark (http://en.wikipedia.org/wiki/Wireshark),
than logs of a Proxy... Not sure wether Wireshark works under Windows,
though.
On a point of information, Wireshark wokrs very effectively under
Windows. The only thing you shouldn't expect to be able to do is tap
into the loopback network, and that's down to the Windows driver structure.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 22 '07 #4
On Sun, 22 Apr 2007 18:07:57 -0400, Steve Holden <st***@holdenweb.com>
wrote:
>On a point of information, Wireshark wokrs very effectively under
Windows. The only thing you shouldn't expect to be able to do is tap
into the loopback network, and that's down to the Windows driver structure.
Thanks for the tip. Someone mentionned a lighter alternative to
display what goes on between browser and web server:

PocketSoap's TCPTrace
http://www.pocketsoap.com/tcptrace/
Apr 24 '07 #5

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

Similar topics

6
by: Sridhar R | last post by:
I am looking for a class browser that has these features. 1. Given a symbol (class, method or function) it should giveback the lineno n source code 2. It should be efficient and quick. I...
40
by: Tools | last post by:
What's the best browser to test for website accessibility? Is there a free screen reader I can use to test how my site reads best?
7
by: Christopher Benson-Manica | last post by:
Why is building a table with the DOM slower than using an array? IOW, why is var table=document.createDocumentFragment(); for( var i=0; i < 4000; i++ ) { tr=table.insertRow( table.rows.length...
16
by: sirsean | last post by:
Hi all. I'm trying to dynamically build menus and objects after my page loads. Data is stored in an XML file and is parsed at runtime into Javascript objects. At the moment, I'm working on creating...
17
by: Nick | last post by:
I am doing some research into building web applications using Object Oriented techniques. I have found the excellent patterns section on the MSDN site, but other than that I cannot find any good,...
3
by: DaveF | last post by:
I need to run a report and then dynamically make a PDF to print. I am looking for examples. Is there any free ways to do this -- David Fetrow Helixpoint LLC. davef@helixpoint.com
3
by: thomson | last post by:
Hi All, While Iam building my solution, the VS 2003 editor get stucked, for a few minutes and after sometimes it gets recovered, and the solution building done, While building i looked into the...
2
by: harry | last post by:
Hi all I have a browser based application that I am looking to deploy. I have built an installer but need to have the installer do some permission changes. I am looking for someone to point me...
1
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
What are the data access choices that I have in building a client-server application with .net 2.0? If the application does not need to support running from browser, do I still need to understand...
3
Plater
by: Plater | last post by:
Well, I'm not sure where to put this, but I figured people using ajax are more likely to run into these issues. I have been building my own webserver (don't ask, just trust me when I say I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.