小编cha*_*zhi的帖子

ValueError:Python2.6.6格式的零长度字段名称

我用这个python shell生成一个字符串:

>>>':'.join("{:x}\n".format(random.randint(0, 2**16 - 1)) for i in range(4))
Run Code Online (Sandbox Code Playgroud)

当我在Python2.7.5中运行这个shell时,一切都顺利.但它发生ValueError: zero length field name in format在Python版本时2.6.6.当Python版本出现时,我该怎么办这个shell 2.6.6呢?

python format

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

Argparse"ArgumentError:参数-h/ - help:冲突的选项字符串:-h, - help"

最近,我正在学习argparse模块,代码下面发生了Argument错误

import argparse
import sys


class ExecuteShell(object):
    def create(self, args):
        """aaaaaaa"""
        print('aaaaaaa')
        return args

    def list(self, args):
        """ccccccc"""
        print('ccccccc')
        return args

    def delete(self, args):
        """ddddddd"""
        print('ddddddd')
        return args


class TestShell(object):
    def get_base_parser(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('-h',
                            '--help',
                            action='store_true',
                            help=argparse.SUPPRESS)

        parser.add_argument('-c', action='store',
                            dest='create_value',
                            help='create a file')

        parser.add_argument('-d', action='store',
                            dest='delete_value',
                            help='delete a file')

        parser.add_argument('-l', action='store',
                            dest='list_value',
                            help='list a dir')

        return parser

    def _find_actions(self, subparsers, actions_module):
        for attr in (action for action in dir(actions_module) if not  action.startswith('__')):
            callback = getattr(actions_module, attr)
            desc = …
Run Code Online (Sandbox Code Playgroud)

python argparse

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

Python随机生成字符串的IP地址

在Python中,如果我想以IP地址的形式生成随机字符串,该怎么办?

例如:"10.0.1.1","10.0.3.14","172.23.35.1"等.

有人可以给我一些帮助吗?

python ip-address

16
推荐指数
6
解决办法
2万
查看次数

使用Python创建自签名X509证书

我按照这个URL创建了一个X509证书.代码是:

from OpenSSL import crypto, SSL
from socket import gethostname
from pprint import pprint
from time import gmtime, mktime

CERT_FILE = "selfsigned.crt"
KEY_FILE = "private.key"

def create_self_signed_cert():

    # create a key pair
    k = crypto.PKey()
    k.generate_key(crypto.TYPE_<wbr>RSA, 1024)

    # create a self-signed cert
    cert = crypto.X509()
    cert.get_subject().C = "UK"
    cert.get_subject().ST = "London"
    cert.get_subject().L = "London"
    cert.get_subject().O = "Dummy Company Ltd"
    cert.get_subject().OU = "Dummy Company Ltd"
    cert.get_subject().CN = gethostname()
    cert.set_serial_number(1000)
    cert.gmtime_adj_notBefore(0)
    cert.gmtime_adj_notAfter(10*<wbr>365*24*60*60)
    cert.set_issuer(cert.get_<wbr>subject())
    cert.set_pubkey(k)
    cert.sign(k, 'sha1')

    open(CERT_FILE, "wt").write(
        crypto.dump_certificate(<wbr>crypto.FILETYPE_PEM, cert)) …
Run Code Online (Sandbox Code Playgroud)

python

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

Python struct.calcsize长度

>>>import struct
>>>size_a = struct.calcsize('10s')
size_a = 10
>>>size_b = struct.calcsize('iii')
size_b = 12
>>>size_c = struct.calcsize('10siii')
size_c = 24
Run Code Online (Sandbox Code Playgroud)

有人能告诉我为什么size_c 24而不是22(10 + 12)?

python

8
推荐指数
1
解决办法
3485
查看次数

Haproxy 503服务不可用。没有服务器可用于处理此请求

haproxy如何处理静态文件(如.css,.js,.jpeg)?当我使用配置文件时,浏览器会说:

503服务不可用

没有服务器可用于处理此请求。

这是我的配置:

global
  daemon
  group root
  maxconn  4000
  pidfile  /var/run/haproxy.pid
  user root

defaults
  log  global
  option  redispatch
  maxconn 65535
  contimeout 5000
  clitimeout 50000
  srvtimeout 50000
  retries  3
  log 127.0.0.1 local3
  timeout  http-request 10s
  timeout  queue 1m
  timeout  connect 10s
  timeout  client 1m
  timeout  server 1m
  timeout  check 10s


listen dashboard_cluster :8888
  mode http
  stats refresh 5s
  balance roundrobin
  option httpclose
  option tcplog
  #stats realm Haproxy \ statistic
  acl url_static path_beg -i /static
  acl url_static path_end -i .css .jpg …
Run Code Online (Sandbox Code Playgroud)

haproxy

6
推荐指数
2
解决办法
3万
查看次数

Postfix无法从文件/etc/ssl/private/server.key获取RSA私钥:禁用TLS支持

我安装了一个postfix邮件服务器.但是当我使用thunderbird登录用户时,这是错误的.这是配置.

postconf -n:

alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
broken_sasl_auth_clients = yes
command_directory = /usr/sbin
config_directory = /etc/postfix
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
debug_peer_level = 2
home_mailbox = Maildir/
html_directory = no
inet_interfaces = all
inet_protocols = all
mail_owner = postfix
mailbox_size_limit = 20000000000
mailq_path = /usr/bin/mailq.postfix
manpage_directory = /usr/share/man
message_size_limit = 200000000
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mydomain = stack.daolicloud.com
myhostname = mail.stack.daolicloud.com
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
myorigin = $mydomain
newaliases_path = /usr/bin/newaliases.postfix
queue_directory = /var/spool/postfix …
Run Code Online (Sandbox Code Playgroud)

linux ubuntu ssl postfix-mta

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

法官Python str包括中文

我想定义一个函数,检查一个字符串是否包含在中文中.例如,check_contain_chinese("中国"),它返回True.check_contain_chinese('xx中国'),它返回True,check_contain_chinese("xxx"),返回False.有人可以给我一些建议吗?我是大一新生..

python

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

Django TemplateDoesNotExist

我建立了一个名为"mysite"的django项目.我的urls.py是

from django.conf.urls import patterns, include, url
from .views import WelcomeView
urlpatterns = patterns('',
    url(r'^$', WelcomeView.as_view(), name='welcome'),
)
Run Code Online (Sandbox Code Playgroud)

我的views.py是:

from django.views import generic
class WelcomeView(generic.TemplateView):
    template_name = 'templates/welcome.html'  
Run Code Online (Sandbox Code Playgroud)

目录结构是

+/home/mysite
    - manage.py
    - static
    + mysite
        - views.py
        - urls.py
        - settings.py
        - __init__.py
        + templates
            - welcome.html
Run Code Online (Sandbox Code Playgroud)

当我跑:

python manage.py runserver
Run Code Online (Sandbox Code Playgroud)

我的浏览器说:"TemplateDoesNotExist at templates/welcome.html".有人可以帮帮我吗?非常感谢 !

python django

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

Python ImportError:无法导入名称itemgetter

Python 2.7.5 (default, Sep 12 2013, 12:43:04) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/urllib.py", line 33, in <module>
  from urlparse import urljoin as basejoin
File "/usr/local/lib/python2.7/urlparse.py", line 118, in <module>
  from collections import namedtuple
File "/usr/local/lib/python2.7/collections.py", line 9, in <module>
  from operator import itemgetter as _itemgetter, eq as _eq
ImportError: cannot import name itemgetter …
Run Code Online (Sandbox Code Playgroud)

python urllib

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

标签 统计

python ×8

argparse ×1

django ×1

format ×1

haproxy ×1

ip-address ×1

linux ×1

postfix-mta ×1

ssl ×1

ubuntu ×1

urllib ×1