Processing a remote iCal with Python
Hi I need to process a remote .ical file from another server on my own server and hand it back to the requesting user.
So what I need to do is:
- Fetch the file (could do this with urllib2)
- Process the file with some regex (not the question)
- hand it back with a specific h开发者_如何学Pythoneader <-- my main problem
I'd know how to do this with php:
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=calendar.ics');
echo $ical;
exit;
But how to do the same thing in python?
Well, as with anything web-related, you start with a WSGI application.
def app(environ, start_response):
calendar = get_processed_file()
start_response('200 OK', [
('Content-Type', 'text/icalendar; charset=utf-8'),
('Content-Disposition', 'inline; filename=calendar.ics'),
])
yield calendar
Then to deploy, you use a handler, e.g. plain CGI would be
import wsgiref
wsgiref.CGIHandler().run(app)
See http://www.wsgi.org/ for more materials about WSGI.
精彩评论