Skip to content
项目

cloudflare/python-worker-hello-world

在 GitHub 查看

关于

Python hello world for Cloudflare Workers

开发工具

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
正在收集历史
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

Python hello world for Cloudflare Workers

Your Python code in index.py, running on Cloudflare Workers.

In addition to Wrangler and npm, you will need to install Transcrypt, including Python 3.7 and virtualenv.

Wrangler

  • Clone repository (git clone https://github.com/cloudflare/python-worker-hello-world)
  • Run npm install
  • Update wrangler.toml with your project name, account_id, and route as required

Further documentation for Wrangler can be found here.

Transcrypt

Before building your project, you'll need to do one-time setup of Transcrypt. Assuming you have Python 3.7 and virtualenv installed per the linked instructions above, that setup on unix systems looks like the following (for windows see virtualenv docs):

cd projectname

virtualenv env

source env/bin/activate

pip install transcrypt

After that you can run Wrangler commands, such as wrangler publish to push your code to Cloudflare. If you exit virtualenv (deactivate) and return to the project directory later, you'll need to activate virtualenv (source env/bin/activate) but will not need to rerun the other installation commands.

If python3 is not Python 3.7 on your system, make sure you install it, create the virtualenv using the right version of Python, and edit webpack.config.js under command to specify the correct path to the Python 3.7 executable in the virtualenv directory. If you are using Windows, see this workaround for an issue with transcrypt-loader paths.

For more information on how Python translates to Javascript, see the Transcrypt docs. especially the module mechanism and aliases.

Because of aliases, for a KV namespace binding named KV you can use KV.put normally, but need to use KV.js_get instead of KV.get. For example, a handler using KV might look like:

def handleRequest(request):
    return KV.js_get('foo').then(
        lambda v: __new__(Response('Python Worker hello world! ' + v, {
        'headers' : { 'content-type' : 'text/plain' }
    })))

探索相关项目

加载中…