Force SSL/HTTPS and non-www in web2py

Update 02.08.2018: Code is updated to use just one line to force https:

request.requires_https()

=======

I read this

https://groups.google.com/forum/#!topic/web2py/RzJ4pYtAWF4

and

https://stackoverflow.com/questions/26802850/pythonanywhere-web2py-redirect-to-https

then I realized “cronjob” problem described in these answers are no longer relevant since `scheduler` has long since replaced `cron`

I created a new model secure.py and add:

########## FORCED SSL non-www ##########
session.secure()
if not request.is_https:
    redirect(URL(scheme='https', args=request.args, vars=request.vars))
request.requires_https()
if request.env.http_host.startswith("www."):
    redirect(URL(host=request.env.http_host[4:]))
#####################################

I skipped the cron part since scheduler has long since replaced cron in web2py.

I created separated model so I can specify only to have it on my server, not on my local macbook, using .gitignore (see #2 below)

2. I need only to use https at server, not local development environment (127.0.0.1:8000), so I put models/secure.py into my .gitignore

Hope this helps!

China Vibe

Vined / Pixabay

Original: Walk Off The Earth – Summer Vibe
Adapted by Yi Liu

EH-O, EH-O, EH-O, BOP BOP AWAY-O

China vibe, China vibe
I’m looking for a China vibe, got me boarding Hainan Airlines, I gotta mute those twits
Jet lag all day, trying to stay awake, wishing these smog away
I wander around Hou Hai, visit royal palace
Take a walk in Peking U, with nothing else to do
But sitting by Weimin Lake, reading a book, off the girls keeping my eyes
Taking my time

With you by my side
A Mobike ride
Cycling with the girls
Street food at night
China vibe
Looking for a China vibe

Friends paid my meals
No reason to be shamed
Bragging on the table, just listening to you
China vibe
Looking for a China vibe

I’m jonesing for clean fresh air, High Speed Rail all down to HangZhou, I’ll find friend’s place to stay
Gonna somehow, find a public square, dance this old night away
We’re drinking LongJing at Farmer’s House, tea trees all up on the hills, under the bluesky
Taking my time

With you by my side
A wood boat ride
Paddling with the girls
West Lake moonlight
China vibe
Looking for a China vibe

Kid paid our dues
With award she earned at school
Riding on dad’s back, just staring at you
China vibe
Looking for a China vibe

And the train arrives home
But I’ll leave again tomorrow
Oh
EH-O, EH-O, EH-O BOP BOP AWAY-O

With you by my side
A Didi ride
Jamming with the girls
Karaoke all night
China vibe
Looking for a China vibe

Love my parents
Got nothing to prove
Sitting by your side, just listening to you
China vibe
Looking for a China vibe

EH-O, EH-O, EH-O BOP BOP AWAY-O
China vibe, China vibe

EH-O, EH-O, EH-O BOP BOP AWAY-O
China vibe, looking for a China vibe

Securing Web2py with Let’s Encrypt / Certbot

In case anybody wondering the same question about using certbot/let’s encrypt and web2py: how do you serve the .well-known folder for certification, when all the request goes to wsgi handler?

1. use `–standalone` as David advised. But you have to stop the web server every time you renew.

2. instead, you can use the standard `–webroot` method. Here, you need to edit your vhost file be it apache or nginx, add a location block:

Using nginx for example:

location /.well-known {
    root /home/www-data/web2py;
}

This way, nginx will server the .well-known and skip passing it onto uwsgi/web2py. Make sure you restart nginx after making this server conf change and use

sudo certbot certonly/renew --webroot -w /home/www-data/web2py -d example.com -d www.example.com

Passenger WSGI setup with pyenv on Dreamhost for Web2Py

Update 2019.12.06: I have moved from DreamHost to Google Cloud VPS. If this post does not work for you, please see alternative method in a comment by Chris.

I have been using fastcgi+flup (link) to run my web2py apps on shared Dreamhost hosting. But the speed was really mediocre. Dreamhost recommends Passenger WSGI for python application.

After hours of research and testing, I would like to share my experience using pyenv + passenger wsgi + web2py on Dreamhost shared hosting as of 08.2017

What I did differently than most guides online is that I only have to edit .htaccess file (in domain root folder) with: (Credit: ASO)

PassengerEnabled on
PassengerAppRoot /home/%USER%/
PassengerPython /home/%USER%/.pyenv/shims/python

Most importantly and strangely, I didn’t need to modify passenger_wsgi.py (handlers/wsgihandler.py) at all. Most guides online (this; this; and many others) require adding two lines after import os, such as this one:

INTERP = "/home/%USER%/.pyenv/shims/python"
#INTERP is present twice so that the new Python interpreter knows the actual executable path
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)

But I couldn’t make it work if I modify passenger_wsgi.py. It seems .htaccess is sufficient to specify pyenv environment.

Some guides also suggest make a pyenv “virtualenv” within app folder, I found it is not required.

Assuming you already know:

  1. how to use web2py
  2. how to use pyenv on Dreamhost
  3. Read about Passenger on Dreamhost

This is what I did:

  1. Enable passenger on Dreamhost panel. A folder public will be created. Don’t worry.
  2. Put web2py files into your domain root folder, which is parent folder of public
  3. cp handlers/wsgihandler.py passenger_wsgi.py
  4. Create/modify .htaccess in domain root folder as described above.
  5. Created tmp/ folder under my web root folder, did a touch restart.txt to get passenger to reload

Your web2py site should now load in your browser. If you use free “Let’s Encrypt” SSL from Dreamhost panel, you should be able to verify your pyenv python environment from admin interface by accessing https. I did verify mine.

I am not sure ssh tunnel method could serve as verification, because you are serving admin from a separate web2py instance running from your shell.

Hope this will help fellow web2py users. Thank all the web2py team for providing web2py.

Add Bluetooth or Inline Remote Mic Cable to Koss KSC75 Headphone

Koss KSC75 is a legendary on-ear clip headphone. It provides stunning high-fidelity sound with unbeatable price. All the 5 star reviews say it all.

I have two of these. One had a wire problem near the plug. I need to replace the cable. I always want to add inline remote and mic to the KSC75 so I can use the headphone more conveniently with my smartphone.

For the other one, which is still perfectly working, I want to convert to a bluetooth headphone. Here is what I have after some simple soldering.

Opening KSC75:


You carefully remove the ear clip and plastic cover to reveal the solder points.

Inline remote and mic cable KSC75:

I bought this DIY inline remote and mic cable from AliExpress for less than $3.

Bluetooth KSC75:

I bought this DIY bluetooth cable from AliExpress for about $11. It is bluetooth 4.0. Now I have both the convenience of Bluetooth and superior sound of KSC75.

怎样办一个交流型读书会

Book Club by J. Bedford @ Flickr https://flic.kr/p/387xxz

rev.2017.08.08

为什么?

在张罗怎么办一个读书会之前,先要思考下为什么要办读书会。不同于学校图书馆书店等机构办的讲座型读书会,草根的交流型读书会至少有三个好处:

  1. 接触了解更多的书。好书很多:有些书你知道好但没时间读,有些书你知道但不知道它的好,有些好书你完全不知道。在读书会平台下,分享者可以用三十分钟的时间帮你概括一本书。
  2. 接触了解更多的爱书之人。和吃饭打牌插科打诨不同,对书的交流,能更深的了解一个人的价值观和思想层次。

你如今的气质里,藏着你曾走过的路,读过的书和爱过的人。

  1. 锻炼组织领导能力。如何依规管理,如何听取意见,如何让每个人发挥特长,如何营造团结融洽的关系,这些都可以得到锻炼。

要申明一下,我们的交流型读书会不是通常的大家一起读一本书之后交流心得。此种规定太死的读书会有一些问题:

  1. 不是每个人都喜欢规定的书。
  2. 不是每个人都喜欢在规定的状态下读书。
  3. 不是每个人都能在规定的时间内读完。
  4. 不是每个人都能深切体会和表达一本规定给自己的书。

这些限制导致了常见的book club failure — 读书会魔咒。我们的交流型读书会希望5位分享者各带一本自己最喜欢的书,从而真正达到“交流”的目的。

Ref: Why Should You Host a Book Club?: INFOGRAPHIC

要素

核心价值

读书会的核心价值是“书”。读书会的活动应该围绕“书的交流”展开。作为组织者,要防止讨论跑偏,活动沦为吃饭聊天打牌。即使因为节日等需要吃喝玩乐,也要穿插跟书有关的活动。

以人为本

高质量的读书会,离不开高素质的爱书之人。读书会的规则制定,活动管理,友谊交流都要以“去伪存真”为目的。让不合适的人进来有如下坏处:

  1. 浪费参与名额
  2. 拉低活动质量
  3. 驱离高质量的参与者
  4. 增加组织者管理成本

另一方面,一旦有了高素质的群体。“二十一世纪最重要的是人才。” 在规则下,给大家自由空间,放手让大家发挥,你会发现自然而然读书会就迸发出思想碰撞迷人的火花。

依托平台

如果你目前还没有一个爱读书的群体,或者你办的是一个面向公众开放的读书会,你需要找到一个平台来进行推广。

我开始办的时候先从朋友里面找,发现个人的社交圈有限。首先读书的人是少数,其次不是每位读书的朋友都愿意来分享,愿意来分享的也不一定正好有时间。所以,你需要一个足够大的社区来支持。

于是我把读书会办在了Hiking+U大洛杉矶区登山俱乐部旗下。这个平台覆盖了本地1000+热爱户外活动充满正能量的华人群体。

另外,我把参加过活动的组成一个Alumni微信群。参加过的人已经用分享和赞助费证明了他们的积极性,并且对活动规则和其他会员相对熟悉。优先让alumni报名可以保证活动的连续性,避免对活动缺乏了解的新人过多。

合理收费

如果你的读书会是一个开放的读书会,从一开始就必须收费。免费活动坏处多:

  1. 免费活动的放鸽子率高达40%。
  2. 免费活动吸引错误的人群。
  3. 免费活动劣币驱逐良币。
  4. 免费活动很难传播组织推广。
  5. 免费活动很难在经济上持续。

详见这篇文章:
To charge or not to charge? The psychology of free vs. paid events

分享者名额重要,我收取$20押金。迟到扣$10, 迟到一刻钟全扣。按时参会退还。

旁听者因为没有分享贡献,收取$10赞助费用。

收上来的费用,主要用于奖励最佳分享者,也可以用于准备食物茶水。我办的读书会全部费用拿来奖励。食物茶水是我个人对大家分享讨论的感谢。

依法治会

制定规则。所有人必须按规则行事,才能有效管理组织。这里特别考验组织者的能力:如何严格执行规则不受他人影响?如何虚心听取建设性的意见?我的心得是:“一切为了读书会”。

严格执行。一部得不到执行的规则是没有意义的。例如,到了点就开始,不能等。

适度调整。根据实际运作的情况和大家的反馈可以调整规则。比如我一开始限制必须是non-fiction非虚构类书籍。后来尝试每次一本fiction,发现fiction也可以结合背景知识展开讲得很精彩。我们就放宽了对fiction的限制。

标准操作规程

通告报名

提前3周的时候,在alumni群(过去参加过活动的成员群)里宣布下期活动的时间地点,征集报名。给alumni群两到三天的deadline. 过之后把剩余的名额放到外部大群中,征集新人报名。

活动时间:

  1. 应选在相对固定的日期。这样大家能够合理的预期并安排自己的日程。我们读书会基本是在每月第二个周日,这个日子和美国的节日冲突较少。若碰到新年、老兵节假期或其他原因报名情况不佳,可以相应调整时间。
  2. 最好选择上午9-12点。
  3. 和普通聚会通常在晚上不同,读书会需要举办在头脑清醒高效的时段。
  4. 把读书会办成早上第一件事,可以避免因其他事情延误导致的迟到。
  5. 办在早上需要早起。可以自然地把动力不足的人筛选掉。肯早起参加的都是真心想来的人。

地点:

选在大家方便的地点即可。设施满足10个人3-4小时的交流。可以在学校公司小区的会议室,也可以轮流办在会员的家里。

人数:

社会学研究表明,为保证高效的群体交流,群体的最佳人数应控制在7±2 也就是 5-9人。相应的,我把读书会的分享者定为5人,旁听4-5人。根据我的经验,经常会有一人出现紧急情况无法赴会。

参考:
The Magic Number For Meeting Attendees
The right size for any party

每次聚会的人数之外,还有一个总群体的人数。这个比较难控制,我们还在摸索阶段。可以参考:

What is the right size for a book club?

收费:

用常用的平台收取押金和赞助费用。作用见上述“要素-合理收费”部分。

书目

报名分享的人应尽快确定所分享书目并告知活动组织者。

准备:

分享者需要准备的内容:

30min is your show time. You decide how to present it. A few examples:
• Read an excerpt (highly encouraged)
• Tell a summary
• Background info about author
• Your reflection
• Online reviews
• Powerpoint slides
• Ask audience questions

分享者水平会高低不齐,大家都能理解。每个人都有一个学习提高的过程。要求分享者提前做一些准备,自己梳理一下书的背景信息,分享内容,主次重点,逻辑条理,主要结论,你的发散思考,还有和听众的互动等。把分享的半小时作为一个锻炼自己public speaking的机会。每一位勇于分享的同学都值得嘉奖,相信适当的准备能让你更有收获!

组织者需要做的准备:

  1. 食物:水果糕点茶水。用来润喉、补充能量。
  2. 制作program。Program用来通知大家分享活动的流程,交通信息等参加活动的注意事项。至少提前一天发给所有人。
  3. 制作award。制作奖状给最佳分享者,名字空出来。等活动末尾选出来之后手写填上最佳分享者名字。
  4. 设好投票。用doodle或类似的投票平台。

活动当天

  1. 到达。不要等迟到的人。一旦等了,以后迟到的会越来越多。
  2. 开场。大家简要的自我介绍。
  3. 上半场。每个人半小时的分享时间。
  4. 休息。
  5. 下半场。
  6. 讨论与主持人的作用。在讨论的过程中,由分享者主导话题。当分享者对讨论失去控制时,主持人要果断介入。比如:1) 讨论偏题或无关话题 2) 讨论单个人用时太长,喧宾夺主,或言之无物 3) 半小时快用完。
  7. 投票选出最佳分享者2人左右,颁奖,合影。

活动之后

  1. 聚餐交流。正式的活动之后已中午。大家可以一起去附近吃饭,在更轻松的氛围下交流下今天的心得或朋友们的近况。
  2. 总结。写一下本次活动的札记,以备存档,公开发布或将来参考。
  3. 下次活动预备。讨论下次活动的备选时间地点和分享主题。
  4. 退还押金给分享者,发放奖金给最佳分享者。

读书会之外

猜书名游戏

  1. 节选段落猜书名:可以完全不给提示,可以口头给提示,可以给选项连线。
  2. 封面、插图猜书名:示例一:封面示例二:插图
  3. 书名谜语
  4. 场景对诗

朗诵会

静默读书会

只读书,不交流,无压力。可以在酒吧,咖啡厅,茶馆和公园进行。

参考 SilentBook.club

 

上面是我办Hiking+U读书会的一点心得,欢迎大家多提宝贵意见。

愿大家都来读书,办读书会,在读书会中以书会友!

《谈写作》二十五句

谈写作 二十五句
谈写作 二十五句

给公号写文章不容易,刚刚在读书会上分享过《谈写作》的我更是压力山大。左思右想决定分享书中每一章的精彩句子,并毫不保留地剖析下自己。

On Writing Well by William Zinsser

PART I Principles

1 The Transaction

Ultimately the product that any writer has to sell is not the subject being written about, but who he or she is.

本文不是在分享《谈写作》,而是我写不出文章偷懒的体现。

2 Simplicity

Very few sentences come out right the first time, or even the third time.

这一句我只写了一遍。

3 Clutter

Be grateful for everything you can throw away.

我把整本书都抛弃了,只留了这25句。感恩吧?!

4 Style

Writing is an act of ego, and you might as well admit it.

好吧,我承认,我写着写着,觉得自己的背影渐渐高大了起来。

5 The Audience

You are writing primarily to please yourself, and if you go about it with enjoyment you will also entertain the readers who are worth writing for.

你读没读完,爱不爱读,没有关系。我交差了,很开心。

6 Words

I write entirely by ear and read everything aloud before letting it go out into the world.

和我一起唱,预备,齐—— let it go, let it go …

7 Usage

Good usage, to me, consists of using good words if they already exist—as they almost always do—to express myself clearly and simply to someone else.

好的用词,不是喜大普奔,也不是人艰不拆,而是“你懂的”。

PART II Methods

8 Unity

As for what point you want to make, every successful piece of nonfiction should leave the reader with one provocative thought that he or she didn’t have before. Not two thoughts, or five—just one.

本文要留给读者的唯一thought就是本书有25个thoughts.

9 The Lead and the Ending

If something surprises you it will also surprise—and delight—the people you are writing for, especially as you conclude your story and send them on their way.

议论文戛然而止式结尾,是行文到了某一地方,作者的观点已经阐明清楚,无庸赘言,这时采取突然收束的一种结尾方法。

10 Bits & Pieces

Go with your interest: no subject is too specialized or too quirky if you make an honest connection with it when you write about it.

写我所爱,爱我所写。我喜欢这本书的内容和作者的行文风格。

PART III Forms

11 Nonfiction as Literature

Ultimately every writer must follow the path that feels most comfortable. For most people learning to write, that path is nonfiction.

Fiction or nonfiction, 这是一个值得考虑的问题.

12 Writing About People: The Interview

Nothing so animates writing as someone telling what he thinks or what he does—in his own words.

一句“洪荒之力”顶上一百句介绍傅园慧的文章。

13 Writing About Places: The Travel Article

“I don’t think children are too young to appreciate what that ship (battleship Arizona at Pearl Harbor) represents,” Magee told me. “They’ll remember it if they see the leaking oil—if they see that the ship is still bleeding.”

向二战烈士致敬!你们的油没有白漏!

14 Writing About Yourself: The Memoir

Memoir isn’t the summary of a life; it’s a window into a life, very much like a photograph in its selective composition.

日记不是流水账,回忆录也不是。

15 Science and Technology

The principle of sequential writing applies to every field where the reader must be escorted over difficult new terrain.

走近科学,靠的是逻辑和条理。

16 Business Writing: Writing in Your Job

If you work for an institution, whatever your job, whatever your level, be yourself when you write.

请参考新闻联播,以及无处不在的各种八股。不要向他们学!

17 Sports

(The best sportswriters) they avoid the exhausted synonyms and strive for freshness elsewhere in their sentences.

德比,绝杀,恶战,潮水般的攻击,可惜只开花不结果,最终饮恨,拭目以待。欢迎补充。

18 Writing About the Arts: Critics and Columnists

“Well,” the man from Texas would break in, “let’s not go peeing down both legs.” (about expressing your opinion firmly.)

尿裤子最好只选一边。

19 Humor

(Humor) it’s an attempt to say important things in a special way that regular writers aren’t getting said in a regular way—or if they are, it’s so regular that nobody is reading it.

本文就是一篇幽默(a joke)。严肃点,不许笑!

PART IV Attitudes

20 The Sound of Your Voice

A woman with taste in clothes delights us with her ability to turn herself out in a combination that’s not only stylish and surprising, but exactly right. She knows what works and what doesn’t.

读到这里的女士,你们都是有品位的女人。

21 Enjoyment, Fear and Confidence

Writers who write interestingly tend to be men and women who keep themselves interested.

读到这里的朋友,你们都是有趣的人。

22 The Tyranny of the Final Product

Moral: any time you can tell a story in the form of a quest or a pilgrimage you’ll be ahead of the game. Readers bearing their own associations will do some of your work for you.

我的神圣理想就是更多的人读这本书,爱上写作。希望不要起到反作用。

23 A Writer’s Decisions

As a nonfiction writer you must get on the plane. If a subject interests you, go after it, even if it’s in the next county or the next state or the next country. It’s not going to come looking for you.

咱们LAX见~

24 Writing Family History and Memoir

Your biggest stories will often have less to do with their subject than with their significance—not what you did in a certain situation, but how that situation affected you and shaped the person you became.

我是如何爱上写作的呢?那是一个夏天…

25 Write as Well as You Can

A reporter once asked him how he managed to play so well so consistently, and he (Joe DiMaggio) said: “I always thought that there was at least one person in the stands who had never seen me play, and I didn’t want to let him down.”

我总觉得至少有那么一个读者会一直读到这里。如果是你,希望我没有让你失望。

结尾了,surprise~~ ;D

更多关于上期沙龙的分享记录,请参考原文链接(果蝇单倍体沙龙速记):8-14-16 读书沙龙速记

IMG_2694

置顶图片来自Pixabay.com CC0 Public Domain

Inkscape Design Notes

1. Inkscape document background color does not export to PDF, but only to PNG. Use rectangle with desired color as background.

2. Inkscape does not support CMYK color mode for printing. Convert text to path, then import/open with Scribus to export PDF in CMYK mode. Text in SVG does not import into Scribus well. But convert text to path seems not to preserve bold style …

3. Save a PDF to CMYK with Inkscape using GhostScript command line tool

4. It seems windows and mac style font size slightly different. Perhaps because of the helvetica they use are different?

北京大学标志logo SVG 矢量文件加反白

最近帮校友会制作印刷品,需要用到logo矢量文件。但是北京大学视觉系统只提供了eps文件,而且反白的标志只提供了PNG,没有提供矢量文件。

我在网上用CloudConvert的在线工具把eps转成svg, 然后用inkscape反白处理,并把黑色的logo删除,单留了一个logo.

我把文件分享在这里,希望有同样需要的朋友能省点时间:

北京大学视觉系统标志原稿EPS
北京大学视觉系统标志SVG
北京大学视觉系统标志红色svg
北京大学视觉系统标志反白红底svg