首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与Pythonanywhere同步

与Pythonanywhere同步
EN

Stack Overflow用户
提问于 2018-01-01 04:25:04
回答 3查看 7.6K关注 0票数 7

我想同步pythonanywhere项目与github帐户。就像我在github的项目中做了更改一样,它会在pythonanywhere自动更新。原谅我我是github的新手。

EN

回答 3

Stack Overflow用户

发布于 2019-01-19 14:33:45

我刚为我自己的Pythonanywhere项目解决了这个问题。我不想费心使用SSH密钥,所以我使用了Github和运行在pythonanywhere帐户上的Python脚本。Python脚本在更新源代码时侦听Github发出的web钩子,并在pythonanywhere上执行一个脚本以提取新文件。

下面是一个场景:

  • 我在本地计算机上使用Visual进行开发,并将代码推送到Github存储库
  • Github会自动发出带有json文件的Post-Receiveweb钩子,我在pythonanywhere服务器上收听该文件。
  • 在我的python脚本中,只要触发web钩子URL,我就执行一个pull命令。在那之后,我所有关于pythonanyhwere的文件都是最新的。

提示:

  • 如果您尚未在pythonanywhere项目上启动git,只需打开bash控制台,导航到根文件夹,例如"home/username“并输入git init,然后输入git remote add origin https://github.com/yourusername/yourreponame.git
  • 您可以在github存储库的设置页面中创建Post-Receiveweb钩子。
  • 我使用GitPython包执行拉请求
  • 下面是我在烧瓶web服务器中使用的python代码,用于等待web钩子执行。它基本上执行一个预定义的bash命令,该命令在pythonanywhere文件结构中自动创建,位于.git/hooks/下。这个bash文件将执行一个简单的git pull origin master

我的flask_app.py文件的内容:

代码语言:javascript
复制
from flask import Flask, request
import git

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
    def webhook():
        if request.method == 'POST':
            repo = git.Repo('./myproject')
            origin = repo.remotes.origin
            repo.create_head('master', 
        origin.refs.master).set_tracking_branch(origin.refs.master).checkout()
            origin.pull()
            return '', 200
        else:
            return '', 400

#
# Below here follows you python back-end code
#

如果你需要更多的信息,请告诉我。

票数 8
EN

Stack Overflow用户

发布于 2018-01-01 06:51:01

你可以考虑:

如果您只想在pythonanywhere上进行开发,就需要生成一个SSH密钥,并按照“GitHub”中的建议将公共密钥添加到您的如何在PythonAnywhere中输入和输出代码帐户中。

票数 3
EN

Stack Overflow用户

发布于 2021-01-19 23:43:16

Django

首先,您需要安装gitpython:pip install gitpython

更新views.py

代码语言:javascript
复制
from django.http import HttpResponse
from git import Repo # 
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def webhook(request):
    if request.method == 'POST':
        repo = Repo('./django-schools')
        git = repo.git
        git.checkout('master')
        git.pull()
        return HttpResponse('pulled_success')
    return HttpResponse('get_request', status=400)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48047123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档