Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b8524898
Commit
b8524898
authored
Feb 24, 1994
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added __del__(), close(), cleanup(), retrieve() and open_local_file()
parent
4729a5f2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
90 additions
and
18 deletions
+90
-18
Lib/urlopen.py
Lib/urlopen.py
+90
-18
No files found.
Lib/urlopen.py
View file @
b8524898
...
@@ -32,6 +32,11 @@ def urlopen(url):
...
@@ -32,6 +32,11 @@ def urlopen(url):
if
not
_urlopener
:
if
not
_urlopener
:
_urlopener
=
URLopener
()
_urlopener
=
URLopener
()
return
_urlopener
.
open
(
url
)
return
_urlopener
.
open
(
url
)
def
urlretrieve
(
url
):
global
_urlopener
if
not
_urlopener
:
_urlopener
=
URLopener
()
return
_urlopener
.
retrieve
(
url
)
# Class to open URLs.
# Class to open URLs.
...
@@ -44,10 +49,25 @@ class URLopener:
...
@@ -44,10 +49,25 @@ class URLopener:
def
__init__
(
self
):
def
__init__
(
self
):
self
.
addheaders
=
[]
self
.
addheaders
=
[]
self
.
ftpcache
=
ftpcache
self
.
ftpcache
=
ftpcache
self
.
tempfiles
=
[]
# Undocumented feature: you can use a different
# Undocumented feature: you can use a different
# ftp cache by assigning to the .ftpcache member;
# ftp cache by assigning to the .ftpcache member;
# in case you want logically independent URL openers
# in case you want logically independent URL openers
def
__del__
(
self
):
self
.
close
()
def
close
(
self
):
self
.
cleanup
()
def
cleanup
(
self
):
import
os
for
tfn
in
self
.
tempfiles
:
try
:
os
.
unlink
(
tfn
)
except
os
.
error
:
pass
# Add a header to be used by the HTTP interface only
# Add a header to be used by the HTTP interface only
# e.g. u.addheader('Accept', 'sound/basic')
# e.g. u.addheader('Accept', 'sound/basic')
def
addheader
(
self
,
*
args
):
def
addheader
(
self
,
*
args
):
...
@@ -56,12 +76,7 @@ class URLopener:
...
@@ -56,12 +76,7 @@ class URLopener:
# External interface
# External interface
# Use URLopener().open(file) instead of open(file, 'r')
# Use URLopener().open(file) instead of open(file, 'r')
def
open
(
self
,
url
):
def
open
(
self
,
url
):
import
string
type
,
url
=
splittype
(
unwrap
(
url
))
url
=
string
.
strip
(
url
)
if
url
[:
1
]
==
'<'
and
url
[
-
1
:]
==
'>'
:
url
=
string
.
strip
(
url
[
1
:
-
1
])
if
url
[:
4
]
==
'URL:'
:
url
=
string
.
strip
(
url
[
4
:])
type
,
url
=
splittype
(
url
)
if
not
type
:
type
=
'file'
if
not
type
:
type
=
'file'
name
=
'open_'
+
type
name
=
'open_'
+
type
if
'-'
in
name
:
if
'-'
in
name
:
...
@@ -75,6 +90,32 @@ class URLopener:
...
@@ -75,6 +90,32 @@ class URLopener:
except
socket
.
error
,
msg
:
except
socket
.
error
,
msg
:
raise
IOError
,
(
'socket error'
,
msg
)
raise
IOError
,
(
'socket error'
,
msg
)
# External interface
# retrieve(url) returns (filename, None) for a local object
# or (tempfilename, headers) for a remote object
def
retrieve
(
self
,
url
):
type
,
url1
=
splittype
(
unwrap
(
url
))
if
not
type
or
type
==
'file'
:
try
:
fp
=
self
.
open_local_file
(
url1
)
return
splithost
(
url1
)[
1
],
None
except
IOError
,
msg
:
pass
fp
=
self
.
open
(
url
)
import
tempfile
tfn
=
tempfile
.
mktemp
()
self
.
tempfiles
.
append
(
tfn
)
tfp
=
open
(
tfn
,
'w'
)
bs
=
1024
*
8
block
=
fp
.
read
(
bs
)
while
block
:
tfp
.
write
(
block
)
block
=
fp
.
read
(
bs
)
headers
=
fp
.
info
()
fp
.
close
()
tfp
.
close
()
return
tfn
,
headers
# Each method named open_<type> knows how to open that type of URL
# Each method named open_<type> knows how to open that type of URL
# Use HTTP protocol
# Use HTTP protocol
...
@@ -100,15 +141,20 @@ class URLopener:
...
@@ -100,15 +141,20 @@ class URLopener:
# Use local file or FTP depending on form of URL
# Use local file or FTP depending on form of URL
def
open_file
(
self
,
url
):
def
open_file
(
self
,
url
):
try
:
return
self
.
open_local_file
(
url
)
except
IOError
:
return
self
.
open_ftp
(
url
)
# Use local file
def
open_local_file
(
self
,
url
):
host
,
file
=
splithost
(
url
)
host
,
file
=
splithost
(
url
)
if
not
host
:
return
addinfo
(
open
(
file
,
'r'
),
noheaders
())
if
not
host
:
return
addinfo
(
open
(
file
,
'r'
),
noheaders
())
host
,
port
=
splitport
(
host
)
host
,
port
=
splitport
(
host
)
if
not
port
and
socket
.
gethostbyname
(
host
)
in
(
if
not
port
and
socket
.
gethostbyname
(
host
)
in
(
localhost
(),
thishost
()):
localhost
(),
thishost
()):
try
:
fp
=
open
(
file
,
'r'
)
return
addinfo
(
open
(
file
,
'r'
),
noheaders
())
except
IOError
:
fp
=
None
raise
IOError
,
(
'local file error'
,
'not on local host'
)
if
fp
:
return
addinfo
(
fp
,
noheaders
())
return
self
.
open_ftp
(
url
)
# Use FTP protocol
# Use FTP protocol
def
open_ftp
(
self
,
url
):
def
open_ftp
(
self
,
url
):
...
@@ -199,8 +245,8 @@ class ftpwrapper:
...
@@ -199,8 +245,8 @@ class ftpwrapper:
raise
IOError
,
(
'ftp error'
,
reason
)
raise
IOError
,
(
'ftp error'
,
reason
)
if
not
conn
:
if
not
conn
:
# Try a directory listing
# Try a directory listing
if
file
:
cmd
=
'
NL
ST '
+
file
if
file
:
cmd
=
'
LI
ST '
+
file
else
:
cmd
=
'
NL
ST'
else
:
cmd
=
'
LI
ST'
conn
=
self
.
ftp
.
transfercmd
(
cmd
)
conn
=
self
.
ftp
.
transfercmd
(
cmd
)
return
addclosehook
(
conn
.
makefile
(
'r'
),
self
.
ftp
.
voidresp
)
return
addclosehook
(
conn
.
makefile
(
'r'
),
self
.
ftp
.
voidresp
)
...
@@ -215,6 +261,11 @@ class addbase:
...
@@ -215,6 +261,11 @@ class addbase:
def
__del__
(
self
):
def
__del__
(
self
):
self
.
close
()
self
.
close
()
def
close
(
self
):
def
close
(
self
):
self
.
read
=
None
self
.
readline
=
None
self
.
readlines
=
None
self
.
fileno
=
None
self
.
fp
.
close
()
self
.
fp
=
None
self
.
fp
=
None
# Class to add a close hook to an open file
# Class to add a close hook to an open file
...
@@ -227,7 +278,8 @@ class addclosehook(addbase):
...
@@ -227,7 +278,8 @@ class addclosehook(addbase):
if
self
.
closehook
:
if
self
.
closehook
:
apply
(
self
.
closehook
,
self
.
hookargs
)
apply
(
self
.
closehook
,
self
.
hookargs
)
self
.
closehook
=
None
self
.
closehook
=
None
self
.
fp
=
None
self
.
hookargs
=
None
addbase
.
close
(
self
)
# class to add an info() method to an open file
# class to add an info() method to an open file
class
addinfo
(
addbase
):
class
addinfo
(
addbase
):
...
@@ -239,6 +291,7 @@ class addinfo(addbase):
...
@@ -239,6 +291,7 @@ class addinfo(addbase):
# Utilities to parse URLs:
# Utilities to parse URLs:
# unwrap('<URL:type//host/path>') --> 'type//host/path'
# splittype('type:opaquestring') --> 'type', 'opaquestring'
# splittype('type:opaquestring') --> 'type', 'opaquestring'
# splithost('//host[:port]/path') --> 'host[:port]', '/path'
# splithost('//host[:port]/path') --> 'host[:port]', '/path'
# splitport('host:port') --> 'host', 'port'
# splitport('host:port') --> 'host', 'port'
...
@@ -246,6 +299,14 @@ class addinfo(addbase):
...
@@ -246,6 +299,14 @@ class addinfo(addbase):
# splittag('/path#tag') --> '/path', 'tag'
# splittag('/path#tag') --> '/path', 'tag'
# splitgophertype('/Xselector') --> 'X', 'selector'
# splitgophertype('/Xselector') --> 'X', 'selector'
def
unwrap
(
url
):
import
string
url
=
string
.
strip
(
url
)
if
url
[:
1
]
==
'<'
and
url
[
-
1
:]
==
'>'
:
url
=
string
.
strip
(
url
[
1
:
-
1
])
if
url
[:
4
]
==
'URL:'
:
url
=
string
.
strip
(
url
[
4
:])
return
url
_typeprog
=
regex
.
compile
(
'^
\
([^/:]+
\
):
\
(.*
\
)$'
)
_typeprog
=
regex
.
compile
(
'^
\
([^/:]+
\
):
\
(.*
\
)$'
)
def
splittype
(
url
):
def
splittype
(
url
):
if
_typeprog
.
match
(
url
)
>=
0
:
return
_typeprog
.
group
(
1
,
2
)
if
_typeprog
.
match
(
url
)
>=
0
:
return
_typeprog
.
group
(
1
,
2
)
...
@@ -291,10 +352,21 @@ def test():
...
@@ -291,10 +352,21 @@ def test():
'gopher://gopher.cwi.nl/11/'
,
'gopher://gopher.cwi.nl/11/'
,
'http://www.cwi.nl/index.html'
,
'http://www.cwi.nl/index.html'
,
]
]
for
arg
in
args
:
try
:
print
'-'
*
10
,
arg
,
'-'
*
10
for
url
in
args
:
print
regsub
.
gsub
(
'
\
r
'
,
''
,
urlopen
(
arg
).
read
())
print
'-'
*
10
,
url
,
'-'
*
10
fn
,
h
=
urlretrieve
(
url
)
print
fn
,
h
if
h
:
print
'======'
for
k
in
h
.
keys
():
print
k
+
':'
,
h
[
k
]
print
'======'
fp
=
open
(
fn
,
'r'
)
data
=
fp
.
read
()
print
regsub
.
gsub
(
'
\
r
'
,
''
,
data
)
print
'-'
*
40
print
'-'
*
40
finally
:
_urlopener
.
cleanup
()
# Run test program when run as a script
# Run test program when run as a script
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment