使用Python处理远程iCal

时间:2011-09-15 11:11:21

标签: python http icalendar

您好我需要从我自己服务器上的另一台服务器处理远程.ical文件,然后将其交还给请求用户。

所以我需要做的是:

  • 获取文件(可以使用urllib2执行此操作)
  • 使用一些正则表达式处理文件(不是问题)
  • 使用特定标题&l​​t; - 我的主要问题
  • 将其移回

我知道如何用php执行此操作:

header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=calendar.ics');
echo $ical;
exit;

但是如何在python中做同样的事情?

1 个答案:

答案 0 :(得分:1)

嗯,与任何与网络相关的内容一样,您可以从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

然后要部署,您使用处理程序,例如普通的CGI将是

import wsgiref
wsgiref.CGIHandler().run(app)

有关WSGI的更多资料,请参阅http://www.wsgi.org/