Results 1 to 7 of 7

Thread: HTTP Header Response [Python] Share/Save - My123World.Com!

  1. #1

    HTTP Header Response [Python]

    Thought of getting an idea into a code ! Here's a small piece of code that will fetch you HTTP headers of a site. Just a small effort in the journey of python

    Code:
    #!/usr/bin/python
    print ("\n*** HTTP Header Response***\n") 
    import urllib2
    host=raw_input("\nEnter the URL\n") 
    host = "http://"+host 
    print "Result:"
    usock = urllib2.urlopen (host)
    print usock.info()
    The three great essentials to achieve anything worth while are: Hard work, Stick-to-itiveness, and Common sense. - Thomas A. Edison
    __________________________________________________ _____________________

  2. #2
    ... I am no Expert b0nd.g4h@gmail.com b0nd's Avatar
    Join Date
    Jul 2010
    Location
    irc.freenode.net #g4h
    Posts
    744
    Quote Originally Posted by prashant_uniyal View Post
    Thought of getting an idea into a code ! Here's a small piece of code that will fetch you HTTP headers of a site. Just a small effort in the journey of python

    Code:
    #!/usr/bin/python
    print ("\n*** HTTP Header Response***\n") 
    import urllib2
    host=raw_input("\nEnter the URL\n") 
    host = "http://"+host 
    print "Result:"
    usock = urllib2.urlopen (host)
    print usock.info()
    Hi Prashant, since you are learning programming so following are my feed-backs:

    1. In programming, the statements like declaration of some global variables, function definitions, constants, imports(in python) etc. should be mentioned on the top. So in your code, "import urllib2" should be before the first print statement. That's just a standard not compulsion.

    2. The above code should check whether "http" is already been fetched by user on not. In current case, if I pass the URL: http://www.xyz.com, the code will fail. Bottom line is, always code in some checks to cope up with user input.

    Rgds
    [*] To follow the path: look to the master, follow the master, walk with the master, see through the master,
    ------> become the master!!! <------
    [*] Everyone has a will to WIN but very few have the will to prepare to WIN
    [*] Invest yourself in everything you do, there's fun in being serious

  3. #3
    Thanks a lot bro..I will keep these all necessary steps in mind the next time I code
    The three great essentials to achieve anything worth while are: Hard work, Stick-to-itiveness, and Common sense. - Thomas A. Edison
    __________________________________________________ _____________________

  4. #4
    Hello Prashant;
    You might like to have this reference book in hands for basic operations in python.
    http://diveintopython.org

    You might also like to have Crunchy for learning.

    Ro(Ha)ck On !!

    Regards;

    Godwin Austin
    Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed

  5. #5
    Thanks godwin bro..I"ll try both
    The three great essentials to achieve anything worth while are: Hard work, Stick-to-itiveness, and Common sense. - Thomas A. Edison
    __________________________________________________ _____________________

  6. #6
    Super Commando Dhruv abhaythehero's Avatar
    Join Date
    Sep 2010
    Location
    Lucknow/Pune,India
    Posts
    466
    Blog Entries
    2
    And here is a good article written by Akash Mahajan which talks about special headers now included in standards for HTTP Response. This headers help in preventing various web attacks. >> http://resources.infosecinstitute.co...ponse-headers/
    In the world of 0s and 1s, are you a zero or The One !

  7. #7
    Garage Member
    Join Date
    Aug 2012
    Location
    India
    Posts
    97
    Blog Entries
    1
    Similar code can be written using Requests library as:

    Code:
    #!/usr/bin/env python
    import requests
    
    h = raw_input("Specify website: ")
    r = requests.get(h)
    print r.headers
    The reason I prefer using Requests over Urllib is that it is pretty simple and straightforward to use.

    Cheers.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who
    keeps learning stays young. The greatest thing in life is to keep your mind young.
    - Henry Ford

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •