Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old January 22nd, 2004, 07:42 AM
snakey snakey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 snakey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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 ?

Reply With Quote
  #2  
Old January 22nd, 2004, 09:03 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old January 22nd, 2004, 09:17 AM
snakey snakey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 snakey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old January 22nd, 2004, 10:30 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Mmmm, ok this is pretty strange, what exactly does your program output... maybe you could attach the whole thing to this thread?

Mark.

Reply With Quote
  #5  
Old January 22nd, 2004, 10:37 AM
snakey snakey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 snakey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old January 22nd, 2004, 03:40 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #7  
Old January 22nd, 2004, 04:10 PM
snakey snakey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 snakey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old January 22nd, 2004, 05:16 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old January 23rd, 2004, 03:26 AM
snakey snakey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 snakey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #10  
Old January 23rd, 2004, 04:56 AM
snakey snakey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 snakey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #11  
Old January 23rd, 2004, 10:40 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #12  
Old January 23rd, 2004, 10:47 AM
snakey snakey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 snakey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #13  
Old January 23rd, 2004, 11:07 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Ah no problem, we'll get it eventually dont worry... let me know how it goes.

Mark.

Reply With Quote
  #14  
Old January 26th, 2004, 03:50 AM
snakey snakey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 snakey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #15