我正在使用微信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)
我无法发布数据
我正在尝试创建一个类似的命令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 …
我正在关注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) 根目录 = /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
我知道文件打开读/写的最佳方法是使用 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) 我正在尝试为 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
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但是我收到此错误,不知道为什么。任何帮助,将不胜感激。
我有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 ×6
python-3.x ×4
django ×2
aiohttp ×1
ansible ×1
cassandra ×1
curl ×1
django-admin ×1
httpserver ×1
nginx ×1
python-2.7 ×1
webserver ×1
wechat ×1