January 22nd, 2004, 08:42 AM
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 ?
January 22nd, 2004, 10:03 AM
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
>>> import urllib
>>> page = urllib.urlopen('http://www.python.org/').read()
>>> print page
January 22nd, 2004, 10:17 AM
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
PS - newpath is the relative path which i check
y = ['http://whatever.com/directory/', dir1]
path = "".join(y)
k = urllib.urlopen ('http://www.python.org')
open_url = k.read ()
print "Content-Type: text/plain \n\n"
print "<b><blink>DOES NOT EXIST: </blink></b>"
how do i open path ?
even python.org is not opening.
Last edited by netytan; January 22nd, 2004 at 11:02 AM.
January 22nd, 2004, 11:30 AM
Mmmm, ok this is pretty strange, what exactly does your program output... maybe you could attach the whole thing to this thread?
January 22nd, 2004, 11:37 AM
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.
January 22nd, 2004, 04:40 PM
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
January 22nd, 2004, 05:10 PM
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
January 22nd, 2004, 06:16 PM
Ok, little mess around with it. give it a go and let me know, its all been tested with Apache 2 so should work...
What version of Python do you have?
import cgi, os, sys, urllib, urlparse
sys.stderr = sys.stdout
#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 the directory exits then open it using urllib.urlopen().
print 'Directory does not exist..'
if __name__ == '__main__':
print 'Content-Type: text/html\n'
January 23rd, 2004, 04:26 AM
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 04:29 AM.
January 23rd, 2004, 05:56 AM
I used the variable "path" where the actual url is stored and i got this error.
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
Apache version 1.3
Python version 2.2
Last edited by snakey; January 23rd, 2004 at 06:03 AM.
January 23rd, 2004, 11:40 AM
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:
January 23rd, 2004, 11:47 AM
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
January 23rd, 2004, 12:07 PM
Ah no problem, we'll get it eventually dont worry... let me know how it goes.
January 26th, 2004, 04:50 AM
I noticed the problem My directories are named as
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.
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
but does not work either
So i'm on this now
well atleast i figured out something . . yuuhhuuu
January 26th, 2004, 08:01 AM
Use urllib.quote to change all spaces and special characters into their URL-friendly equivalents.
>>> urllib.quote('foo bar')