source: py-scraping/mechanize/_file.py@ 154

Last change on this file since 154 was 106, checked in by Rick van der Zwet, 15 years ago

Initial commit...

File size: 2.1 KB
RevLine 
[106]1try:
2 from cStringIO import StringIO
3except ImportError:
4 from StringIO import StringIO
5import mimetools
6import os
7import socket
8import urllib
9from urllib2 import BaseHandler, URLError
10
11
12class FileHandler(BaseHandler):
13 # Use local file or FTP depending on form of URL
14 def file_open(self, req):
15 url = req.get_selector()
16 if url[:2] == '//' and url[2:3] != '/':
17 req.type = 'ftp'
18 return self.parent.open(req)
19 else:
20 return self.open_local_file(req)
21
22 # names for the localhost
23 names = None
24 def get_names(self):
25 if FileHandler.names is None:
26 try:
27 FileHandler.names = (socket.gethostbyname('localhost'),
28 socket.gethostbyname(socket.gethostname()))
29 except socket.gaierror:
30 FileHandler.names = (socket.gethostbyname('localhost'),)
31 return FileHandler.names
32
33 # not entirely sure what the rules are here
34 def open_local_file(self, req):
35 try:
36 import email.utils as emailutils
37 except ImportError:
38 import email.Utils as emailutils
39 import mimetypes
40 host = req.get_host()
41 file = req.get_selector()
42 localfile = urllib.url2pathname(file)
43 try:
44 stats = os.stat(localfile)
45 size = stats.st_size
46 modified = emailutils.formatdate(stats.st_mtime, usegmt=True)
47 mtype = mimetypes.guess_type(file)[0]
48 headers = mimetools.Message(StringIO(
49 'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
50 (mtype or 'text/plain', size, modified)))
51 if host:
52 host, port = urllib.splitport(host)
53 if not host or \
54 (not port and socket.gethostbyname(host) in self.get_names()):
55 return urllib.addinfourl(open(localfile, 'rb'),
56 headers, 'file:' + file)
57 except OSError, msg:
58 # urllib2 users shouldn't expect OSErrors coming from urlopen()
59 raise URLError(msg)
60 raise URLError('file not on local host')
Note: See TracBrowser for help on using the repository browser.