Quantcast

Jump to content


Photo

Python http wrapper


  • Please log in to reply
5 replies to this topic

#1 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 14 November 2010 - 09:57 PM

I'm trying to write a short python http wrapper to show at my interview. Not really having much luck so I was hoping someone can tell me what I'm doing wrong:

import urllib2

def main():
print http_req("http://www.google.ca/", None, "")


def http_req(url, data, last_page):
req = urllib2.Request(url, data)
req.add_header("Referer", referer)
req.add_header("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 AskTbBT5/3.9.1.14019 Firefox/3.6.12")

dpage = urllib2.urlopen(req)

resp_data = dpage.read()

return resp_date.encode('utf-8')

I was using some lines from a code that Artifeetfetish posted in this thread.

Anyways I'm not getting anything on the console except "Press any key to continue". I should be getting a print out of google's html shouldn't I?

#2 Pyro699

Pyro699
  • 1543 posts


Users Awards

Posted 14 November 2010 - 10:03 PM

This is what your after :p


import urllib2

def main():
print http_req("http://www.google.ca/", None, "")


def http_req(url, data, last_page):
req = urllib2.Request(url, data)

#Dont set a refer header if there is no refer data to set :p
if last_page != "":
#You should also set that to last_page :p not referer
req.add_header("Referer", last_page)

req.add_header("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 AskTbBT5/3.9.1.14019 Firefox/3.6.12")
dpage = urllib2.urlopen(req)
resp_data = dpage.read()

#Change to resp_data :p not #resp_date
#You dont need to encode it... its already good enough to print :p
return resp_data

main()


#3 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 14 November 2010 - 10:08 PM

This is what your after :p


import urllib2

def main():
print http_req("http://www.google.ca/", None, "")


def http_req(url, data, last_page):
req = urllib2.Request(url, data)

#Dont set a refer header if there is no refer data to set :p
if last_page != "":
#You should also set that to last_page :p not referer
req.add_header("Referer", last_page)

req.add_header("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 AskTbBT5/3.9.1.14019 Firefox/3.6.12")
dpage = urllib2.urlopen(req)
resp_data = dpage.read()

#Change to resp_data :p not #resp_date
#You dont need to encode it... its already good enough to print :p
return resp_data

main()



Darn stupid mistakes >_< thanks pyro, but I'm getting the same results...

edit: nvm, it worked after I added that "main()" at the end. What is that for anyways?



#4 Pyro699

Pyro699
  • 1543 posts


Users Awards

Posted 14 November 2010 - 10:10 PM

Bah :p you replied too quickly...

you defined main as a function... i think what you meant to put is:

if __name__ == "__main__":
print http_req("http://www.google.ca/", None, "")


but in all honesty, since this is SO simple :p you don't even need that if statement... you can just run it as is... i really think you need to read a few books on python before going to a job interview for this language :p


import urllib2

def main():
print http_req("http://www.google.ca/", None, "")


def http_req(url, data, last_page):
req = urllib2.Request(url, data)

#Dont set a refer header if there is no refer data to set :p
if last_page != "":
#You should also set that to last_page :p not referer
req.add_header("Referer", last_page)

req.add_header("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 AskTbBT5/3.9.1.14019 Firefox/3.6.12")
dpage = urllib2.urlopen(req)
resp_data = dpage.read()

#Change to resp_data :p not #resp_date
#You dont need to encode it... its already good enough to print :p
return resp_data

#This works
main()

#And this
print http_req("http://www.google.ca/", None, "")

#And this
if __name__ == "__main__":
print http_req("http://www.google.ca/", None, "")

Edited by Pyro699, 14 November 2010 - 10:13 PM.


#5 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 14 November 2010 - 10:12 PM

Then you must be running it wrong :p cause if i was to copy/paste that into a script and hit run... it will work xD


Sorry it was a mistake on my part, read my edit. :p

All that's left is pad it out to make it look longer :p

#6 Pyro699

Pyro699
  • 1543 posts


Users Awards

Posted 14 November 2010 - 10:13 PM

Refresh xP


2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users