小编Mat*_*our的帖子

Python定时套接字连接

我希望花时间完成使用TCP的服务器往返.使用Windows客户端时.(我会使用ping,但服务器阻止这个)

我正在寻找使用python和套接字来完成这个,我现在有.

import time
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )

start = time.time()
s.connect(('localhost',80))
print 'time taken ', time.time()-start ,' seconds'

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

我的问题是我不认为连接计时器正在工作,因为我经常得到相同的时间戳返回.有人能指出我正确的方向来解决这个问题.

python sockets windows network-programming

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

Windows多处理

正如我所发现的,在进行多处理时,windows有点麻烦,对此我有一个疑问。

pydoc指出,使用多重处理时,您应该保护Windows应用程序的入口点

这是否仅意味着创建新流程的代码?

例如

脚本1

import multiprocessing

def somemethod():
    while True:
        print 'do stuff'

# this will need protecting
p = multiprocessing.Process(target=somemethod).start()

# this wont
if __name__ == '__main__':
    p = multiprocessing.Process(target=somemethod).start()
Run Code Online (Sandbox Code Playgroud)

在此脚本中,您需要将其包装在if main中,因为产生该过程的行。 但是,如果有的话呢?

剧本2

file1.py

import file2
if __name__ == '__main__':
    p = Aclass().start()
Run Code Online (Sandbox Code Playgroud)

file2.py

import multiprocessing
ITEM = 0
def method1():
    print 'method1'

method1()

class Aclass(multiprocessing.Process):
    def __init__(self):
        print 'Aclass'
        super(Aclass, self).__init__()

    def run(self):
        print 'stuff'
Run Code Online (Sandbox Code Playgroud)

在这种情况下需要保护什么? 如果在文件2中有一个if __main__会发生什么,如果正在创建一个进程,该代码中的代码会被执行吗?

注意:我知道代码将无法编译。这只是一个例子。

python windows multiprocessing

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

绑定到光标移动不会更改INSERT标记

每次用户通过箭头,鼠标点击等更改插入点时,我都需要执行快速检查...所以我这样绑定它:

text.bind("<Button-1>", insertchanged)

def insertchanged(event):
    pos=text.index(INSERT)
    n=text.tag_names(INSERT)
        ...
Run Code Online (Sandbox Code Playgroud)

但我发现在用户改变之前 pos仍然是位置!如何找到新职位(一般解决方案,如果可能的话:我必须将其绑定到home,end,pgup,pgdown,...)

谢谢!

python user-interface tkinter

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

Python处理系统关闭

我有一个多处理的python应用程序,它在Windows上作为EXE运行.在选择关闭操作系统时,由于进程被关闭,应用程序会抛出许多异常.

有没有办法通过Windows捕获系统关闭请求,所以我可以自己处理多进程的关闭?

python windows events shutdown multiprocessing

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

使用Delphi通过网络连接到受密码保护的共享文件夹

我有一个多用户Delphi程序,需要通过网络共享文件夹来存储数据.我希望程序更改该文件夹中的文件,但不是普通用户(可以看到此文件夹)或网络病毒...

我想用密码保护这个文件夹(Windows 7),但我需要通过我的程序编写新文件或编辑现有文件,我不知道如何做到这一点.

简而言之,我需要通过这样的代码连接和断开共享文件夹

ConnectToFolder(\\myServerMachine\mySharedfolder username:me password:myPassword);
disConnectToFolder(\\myServerMachine\mySharedfolder username:me password:myPassword);
Run Code Online (Sandbox Code Playgroud)

这可能吗?

delphi directory networking shared network-programming

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

Django queryset返回单个值

我有以下内容-

obj.owner_id = Owner.objects.filter(owner_name=owner_obj).values_list('owner_id')[0]
Run Code Online (Sandbox Code Playgroud)

问题在于它返回的值是一个元组。具体来说-

(786,)
Run Code Online (Sandbox Code Playgroud)

我要返回的只是整数值或786

我究竟做错了什么?

python django

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

使用生产服务器上的django日志记录的Apache WSGI权限错误

这是一些软件信息

Django 1.8.1 Apache2 Fedora 21

error_log输出

mod_wsgi (pid=8272): Target WSGI script '/var/www/anime/anime/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=8272): Exception occurred processing WSGI script '/var/www/anime/anime/wsgi.py'.
Traceback (most recent call last):
   File "/usr/lib64/python3.4/logging/config.py", line 557, in configure
     handler = self.configure_handler(handlers[name])
   File "/usr/lib64/python3.4/logging/config.py", line 725, in configure_handler
     result = factory(**kwargs)
   File "/usr/lib64/python3.4/logging/__init__.py", line 999, in __init__
     StreamHandler.__init__(self, self._open())
   File "/usr/lib64/python3.4/logging/__init__.py", line 1023, in _open
     return open(self.baseFilename, self.mode, encoding=self.encoding)
 PermissionError: [Errno 13] Permission denied: '/var/www/anime/log/info.log'

 During handling of the above exception, …
Run Code Online (Sandbox Code Playgroud)

apache django wsgi selinux

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

使用多个区域的局部视图 - MVC4

我想要跨多个区域使用视图.我已将部分视图置于主应用程序Views/Shared文件夹中,但我似乎无法访问它.

有谁知道如何访问共享视图文件夹?

我目前使用的代码是基本的:

@Html.RenderPartial("MyPartialControl")
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc partial-views razor asp.net-mvc-4

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

如何在Django HTML中导入模块?

我有一个django博客。我需要使用附魔来检查博客文章中的文本是否为英文,然后在文本错误更正中使用api。

我将api安装在django博客的虚拟环境中

pip安装pyenchant

项目,并将其包含在已安装的应用程序中,但是在博客base.html中,我尝试加载它并使用它的功能来检查帖子标题是否为英文,但是我什么也没做。如何解决呢?这是我的html代码:

{% load enchant %}

{% dictionary = enchant.Dict("en_US") %}
<p>{% dictionary.check(post.title) %}</p>
Run Code Online (Sandbox Code Playgroud)

当我运行服务器时没有错误,但是html页面上没有任何内容。注意:根据API,在段落标记中应该为True of False。“我在python shell中对其进行了测试。”

python django django-templates django-models

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

捕获异常时,sys.exit()不会退出程序

该程序检查分子式.我希望程序在检测到公式中的错误时立即退出.例如,公式"a"不正确.当我通过我的代码运行它时:

def readletter():
    if q.peek() in string.ascii_lowercase:
        print(q.peek())
        return q.get()
    else:
        raise Formelfel("Förväntad liten bokstav.")

def readLetter():
    if q.peek() in string.ascii_uppercase:
        print(q.peek())
        return q.get()
    else:
        raise Formelfel("Förväntad stor bokstav.")

def readAtom():
    X = ""
    try:
        X += readLetter()
    except Formelfel:
        print("Missing capital letter at end of row "+getRest())
        sys.exit()
        return

    try:
        x = readletter()
        atom = X+x
    except (Formelfel, TypeError):
        atom = X

    if atom in ATOMER:
        return
    else:
        raise Formelfel("Okänd atom.")

def readGroup():
    if q.peek() in string.ascii_uppercase or q.peek() …
Run Code Online (Sandbox Code Playgroud)

python exit sys

0
推荐指数
1
解决办法
969
查看次数

如何以“Wed Aug 21 2019 8:13 PM”格式打印日期时间?

我正在尝试以以下格式打印时间,我是否要以“2019 年 8 月 21 日星期三晚上 8:13”格式打印日期和时间?

import datetime
now = datetime.datetime.now()
print now.strftime("%m-%d-%Y %H:%M")
Run Code Online (Sandbox Code Playgroud)

电流输出:-

08-21-2019 20:13
Run Code Online (Sandbox Code Playgroud)

预期输出:-

Wed Aug 21 2019 8:13 PM
Run Code Online (Sandbox Code Playgroud)

python datetime

0
推荐指数
1
解决办法
159
查看次数

Django表单,request.post和initial

我有一个django表单,我需要为验证目的设置一个值,该值不作为标准Post请求的一部分传递.

在我的代码中,我目前有类似的东西:

if request.method == 'POST':
        postform = CreatePostForm(request.POST, request.FILES, initial={'post_type':post.post_type})

        if postform.is_valid():
                .....
Run Code Online (Sandbox Code Playgroud)

值post_type是一个值为'QUE'的选择

我遇到的问题是这似乎没有效果.有没有人有任何关于如何在验证发生之前将post_type值添加到CreatePostForm类的建议.

请注意我不想在表单上公开这个值,因此包含它作为帖子的一部分不是一个选项.

forms django validation post

-2
推荐指数
1
解决办法
4582
查看次数