Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0

    Unhappy how to "openurl"


    I dont use python too much, just small basic codes. But now I have a http path stored in a variable. How do i use openurl to open that page directly ?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    By directly assume you mean reading the page.. if so then this should work fine for you!

    Since urllib is a file like object this is pretty much like opeing a local file

    Code:
    >>> import urllib
    >>> page = urllib.urlopen('http://www.python.org/').read()
    >>> print page
    ...
    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0
    I tried exactly the same, but it hasn't worked for me , thats why i posted this question on the list to check if i was doing something wrong.
    here's what i do

    Code:
    y = ['http://whatever.com/directory/', dir1]
    path = "".join(y)
    
    if os.path.isdir(newpath):
        k = urllib.urlopen ('http://www.python.org')
        open_url = k.read ()
        print "Content-Type: text/plain \n\n"
        print open_url
    else:
        print "<b><blink>DOES NOT EXIST: </blink></b>"
    PS - newpath is the relative path which i check

    how do i open path ?
    even python.org is not opening.
    Last edited by netytan; January 22nd, 2004 at 10:02 AM.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Mmmm, ok this is pretty strange, what exactly does your program output... maybe you could attach the whole thing to this thread?

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0
    take input from the user for a directory, if it exists, open it in the url and if not just print a msg saying the directoy does not exist.
    Thats where i join dir1 (user input) to the first part of the url

    i'm using Apache web server.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok so lets see if i'm getting this... what you want the program to do is

    1. ask the user for a directory on the server (relative to the position of the script)

    2. if the directory exists then open the directory (as read by apache) urllib.urlopen()

    3. else print a message telling the user that the page couldn't be loaded.

    right or wrong? Shouldn't be too hard, just check if this is what you wanted and i'll write you an example

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0
    you got it right . I have my program ready foir this, the only thing i need (or dont understand) is the "urlopen" and why the same example you gave is not working
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok, little mess around with it. give it a go and let me know, its all been tested with Apache 2 so should work...

    Code:
    #!/usr/bin/env python
    
    import cgi, os, sys, urllib, urlparse
    
    sys.stderr = sys.stdout
    
    def view():
    	
    	#Load form values and make them available though the 'form' variable. Put
    	#the form value 'url' into 'path'.
    	
    	form = cgi.FieldStorage()
    	path = form['url'].value
    	
    	#Split path using urlparse.urlsplit().
    	
    	split = urlparse.urlsplit(path)
    	
    	if os.path.isdir(split[2][1:]):
    		
    		#If the directory exits then open it using urllib.urlopen().
    		
    		print urllib.urlopen(path).read()
    	else:
    		print 'Directory does not exist..'
    
    if __name__ == '__main__':
    
    	print 'Content-Type: text/html\n'
    	view()
    What version of Python do you have?

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0
    Ok, your code is almost the same as mine
    now just as a test i use the http://www.python.org url
    SO if the directory exists open this url or print directory not found

    here's the error message i get

    Traceback (most recent call last): File "/cgi-bin/test.cgi", line 46, in ? print urllib.urlopen('http://www.python.org').read() File "/usr/lib/python2.1/urllib.py", line 71, in urlopen return _urlopener.open(url) File "/usr/lib/python2.1/urllib.py", line 176, in open return getattr(self, name)(url) File "/usr/lib/python2.1/urllib.py", line 283, in open_http h.putrequest('GET', selector) File "/usr/lib/python2.1/httplib.py", line 437, in putrequest self.send(str) File "/usr/lib/python2.1/httplib.py", line 379, in send self.connect() File "/usr/lib/python2.1/httplib.py", line 363, in connect self.sock.connect((self.host, self.port)) IOError: [Errno socket error] (111, 'Connection refused')
    Last edited by snakey; January 23rd, 2004 at 03:29 AM.
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0
    I used the variable "path" where the actual url is stored and i got this error.

    Not Found
    The requested URL /Temp/Dir1/Dir2 was not found on this server.

    but when i print path, i get the whole url, which i copy paste in the browser and it works

    strange ?

    Apache version 1.3
    Python version 2.2
    Last edited by snakey; January 23rd, 2004 at 05:03 AM.
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Mmmmm ok, thats pretty wierd i agree! Sounds to me like urllib is just broken or somthing. Best bet would be to upgrade you Python version.

    May sound silly but, you're online when you're testing this right?

    Also, you seem to be using Python 2.1 not 2.2 (as indecated in the Traceback) so you might wana check that out!

    You should note that the example i wrote is designed to be used locally! So the url you input should be relative to you're website.. not another site. If you're trying to check if a directory exists on a remote server that could be a little harder and maybe outside the scope of urllib. Just thought i'd mention it just incase

    heres an example of the input required by my example:

    http://localhost/url=http://localhost/dir

    Mark.
    programming language development: www.netytan.com Hula

  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0
    ok , i'll try upgrading my Python version. The directory exists on the same server.

    I wonder why my variable path shows the correct url if i print it out, but not open the url with i use the variable in the urlopen.


    Thanks for all the help . . . although i have a bad feeling, this is not the end

    have a nice weekend
    cheers
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ah no problem, we'll get it eventually dont worry... let me know how it goes.

    Mark.
    programming language development: www.netytan.com Hula

  26. #14
  27. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0
    I noticed the problem My directories are named as
    Temp 1
    Temp 2
    Temp 3

    so there is a space in between and Apache could not resolve that space. I tried to append %20 between Temp and the number. But this didnt work either.

    I do

    y = ['http://whatever.com/Directory/Temp', input_dir_no]
    path = "%20".join(y)

    input_dir_no i get from the submit

    which should resolve as
    http://whatever.com/Directory/Temp%201
    but does not work either
    So i'm on this now

    well atleast i figured out something . . yuuhhuuu
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Use urllib.quote to change all spaces and special characters into their URL-friendly equivalents.

    Code:
    >>> urllib.quote('foo bar')
    'foo%20bar'
    >>>
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo