小编mic*_*al 的帖子

使用Python请求中的POST表单数据上载Image

我正在使用微信API ...在这里我要使用这个API将图像上传到微信的服务器 http://admin.wechat.com/wiki/index.php?title=Transferring_Multimedia_Files

    url = 'http://file.api.wechat.com/cgi-bin/media/upload?access_token=%s&type=image'%access_token
    files = {
        'file': (filename, open(filepath, 'rb'),
        'Content-Type': 'image/jpeg',
        'Content-Length': l
    }
    r = requests.post(url, files=files)
Run Code Online (Sandbox Code Playgroud)

我无法发布数据

python curl python-3.x python-requests wechat

21
推荐指数
5
解决办法
6万
查看次数

Django自定义命令错误:无法识别的参数

我正在尝试创建一个类似的命令createsuperuser,它将带有两个参数(用户名和密码)

它在django 1.7中运行良好,但在1.8中没有.(我也使用python3.4)

这是我写的代码

MyApp的/管理/命令/ createmysuperuser.py

from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User


class Command(BaseCommand):
    help = 'Create a super user'

    def handle(self, *args, **options):

        if len(args) != 2:
            raise CommandError('need exactly two arguments for username and password')
        username, password = args

        u, created = User.objects.get_or_create(username=username)
        if created:
            u.is_superuser = True
            u.is_staff = True
            u.set_password(password)
            u.save()
        else:
            raise CommandError("user '%s' already exist" % username)

        return "Password changed successfully for user '%s'" % u.username
Run Code Online (Sandbox Code Playgroud)

当我尝试运行此命令时

$ python …

python django django-admin

17
推荐指数
2
解决办法
1万
查看次数

方法:使用Python3在ansible中使用django_manage

我正在关注django manage.py模块 http://docs.ansible.com/django_manage_module.html

例如,我的任务之一看起来像 -

- name: Django migrate
  django_manage: command=migrate
                 app_path={{app_path}}
                 settings={{django_settings}}
  tags:
    - django
Run Code Online (Sandbox Code Playgroud)

这对python2完全没问题(在ubuntu中默认)但是当我尝试使用python3-django项目时会抛出错误

failed: [123.456.200.000] => (item=school) => {"cmd": "python manage.py makemigrations --noinput school --settings=myproj.settings.production", "failed": true, "item": "school", "path": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games", "state": "absent", "syspath": ["/home/ubuntu/.ansible/tmp/ansible-tmp-1432039779.41-30449122707918", "/usr/lib/python2.7", "/usr/lib/python2.7/plat-x86_64-linux-gnu", "/usr/lib/python2.7/lib-tk", "/usr/lib/python2.7/lib-old", "/usr/lib/python2.7/lib-dynload", "/usr/local/lib/python2.7/dist-packages", "/usr/lib/python2.7/dist-packages"]}
msg: 
:stderr: Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Run Code Online (Sandbox Code Playgroud)

从这个错误看起来Ansible bydefault使用Python2.我们可以将其更改为python3或任何其他解决方法吗?

PS:pip冻结确保安装了django 1.8(对于使用pip3的python3)

建议:当我运行ubuntu@ubuntu:/srv/myproj$ python3 manage.py migrate它工作正常.所以我想把命令直接传递给像

 - name: …
Run Code Online (Sandbox Code Playgroud)

django ansible ansible-playbook

9
推荐指数
1
解决办法
2464
查看次数

在 Nginx 中提供静态 HTML 文件而不在 url 中扩展

根目录 = /srv/myproject/xyz/main/

在“主”文件夹中,我有几个 *.html 文件,我希望它们都指向一个 url 说/test/(这与目录结构完全不同)

这是我非常基本的 nginx 配置

server {
    listen                80;
    error_log   /var/log/testc.error.log;

    location /test/ {
         root   /srv/myproject/xyz/main/;
         #alias /srv/myproject/xyz/main/;
         default_type   "text/html";
         try_files  $uri.html ;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我使用简单别名

location /test/ {
       alias /srv/myproject/xyz/main/;   
}
Run Code Online (Sandbox Code Playgroud)

那么它的工作完美,我的意思是我可以通过http://www.myurl.com/test/firstfile.html等等访问这些html文件

但我不想要那个 html 扩展。

我试图遵循这些线程但没有成功 http://forum.nginx.org/read.php?11,201491,201494

如何使用 NGINX 从 url 中删除 .php 和 .html 扩展名?

如何在 nginx 中提供 html 文件而不在此别名设置中显示扩展名

webserver nginx httpserver

6
推荐指数
1
解决办法
3813
查看次数

Python打开和读取文件一个班轮

我知道文件打开读/写的最佳方法是使用 with

而不是使用

f = open('file.txt', 'w')
f.write('something')
f.close()
Run Code Online (Sandbox Code Playgroud)

我们应该写 -

with open('file.txt', 'w') as f:
    f.write('something')
Run Code Online (Sandbox Code Playgroud)

但是如果我想简单地读一个文件怎么办?我可以做这个

with open('file.txt') as f:
    print (f.read())
Run Code Online (Sandbox Code Playgroud)

但是下面的问题是什么呢?

print (open('file.txt').read())

要么

alist = open('file.txt').readlines()
print (alist)
Run Code Online (Sandbox Code Playgroud)

执行此语句后会自动关闭文件吗?这是一种标准的写作方式吗?我们应该这样写吗?

除此之外 - 我应该在函数中打开一个文件并将指针传递给其他用于写入,还是应该将其声明为全局变量?即

def writeto(f):
    #do some stuff
    f.write('write stuff')

def main():
   f = open('file.txt', 'w')
   while somecondition:
        writeto(f)
   f. close()
Run Code Online (Sandbox Code Playgroud)

要么

f = open('file.txt', 'w')

def writeto():
    #do some stuff
    f.write('write stuff')

def main():
   while somecondition:
        writeto()

f. close()
Run Code Online (Sandbox Code Playgroud)

python python-2.7 python-3.x

5
推荐指数
1
解决办法
5712
查看次数

cassandra.InvalidRequest: code=2200 [无效查询] message="Keyspace '' 不存在"

我正在尝试为 cassandra使用python 驱动程序, 但是当我在 python shell 中运行这三行时

from cassandra.cluster import Cluster
cluster = Cluster()
session = cluster.connect('demo')
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

cassandra.InvalidRequest: code=2200 [Invalid query] message="Keyspace 'demo' does not exist"
Run Code Online (Sandbox Code Playgroud)

pip冻结 说 cassandra-driver==2.5.0

我检查了 cqlsh

Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 2.1.4 | CQL spec 3.2.0 | Native protocol v3]
Use HELP for help.
cqlsh> describe keyspaces

system_traces  system

cqlsh> 
Run Code Online (Sandbox Code Playgroud)

没有名为“演示”的密钥空间,但我只是在关注这两个教程,他们没有说任何关于预创建密钥空间的内容 http://planetcassandra.org/getting-started-with-cassandra-and-python/ http: //datastax.github.io/python-driver/getting_started.html

python cassandra cassandra-2.0

5
推荐指数
1
解决办法
1万
查看次数

尝试使用asyncio时,为什么会出现“ RuntimeError:使用yield而不是Task Task中生成器的yield”的问题?

import asyncio

f = open('filename.txt', 'w')

@asyncio.coroutine
def fun(i):
    print(i)
    f.write(i)
    # f.flush()

def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.as_completed([fun(i) for i in range(3)]))
    f.close()

main()
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用python3的新库。asyncio但是我收到此错误,不知道为什么。任何帮助,将不胜感激。

python python-3.x python-asyncio

4
推荐指数
1
解决办法
695
查看次数

asyncio中的信号量/多个池锁用于1个代理 - aiohttp

我有5,00,000个网址.并希望异步获得每个响应.

import aiohttp
import asyncio    

@asyncio.coroutine
def worker(url):
    response = yield from aiohttp.request('GET', url, connector=aiohttp.TCPConnector(share_cookies=True, verify_ssl=False))
    body = yield from response.read_and_close()

    print(url)

def main():
    url_list = [] # lacs of urls, extracting from a file

    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait([worker(u) for u in url_list]))

main()
Run Code Online (Sandbox Code Playgroud)

我一次想要200个连接(并发200个),而不是因为这个

当我运行这个程序50个网址它工作正常,url_list[:50] 但但如果我通过整个列表,我得到这个错误

aiohttp.errors.ClientOSError: Cannot connect to host www.example.com:443 ssl:True Future/Task exception was never retrieved future: Task()
Run Code Online (Sandbox Code Playgroud)

可能是频率太高,服务器拒绝在限制后做出响应?

python python-3.x python-asyncio aiohttp

3
推荐指数
1
解决办法
1319
查看次数