The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
how to "openurl"
Discuss how to "openurl" in the Python Programming forum on Dev Shed. how to "openurl" Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 22nd, 2004, 07:42 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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, 09:03 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

January 22nd, 2004, 09:17 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation 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.
|

January 22nd, 2004, 10:30 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
Mmmm, ok this is pretty strange, what exactly does your program output... maybe you could attach the whole thing to this thread?
Mark.
|

January 22nd, 2004, 10:37 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation 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.
|

January 22nd, 2004, 03:40 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

January 22nd, 2004, 04:10 PM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation 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 
|

January 22nd, 2004, 05:16 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

January 23rd, 2004, 03:26 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation 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.
|

January 23rd, 2004, 04:56 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation 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.
|

January 23rd, 2004, 10:40 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

January 23rd, 2004, 10:47 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation 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
|

January 23rd, 2004, 11:07 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
Ah no problem, we'll get it eventually dont worry... let me know how it goes.
Mark.
|

January 26th, 2004, 03:50 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation 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
|

January 26th, 2004, 07:01 AM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
Use urllib.quote to change all spaces and special characters into their URL-friendly equivalents.
Code:
>>> urllib.quote('foo bar')
'foo%20bar'
>>>
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|