小编Nic*_*son的帖子

正确关闭插座

我正在尝试使用套接字建立服务器/客户端连接.但他们不能正常关闭,我无法理解为什么.

更新1

我已经纠正了我的愚蠢错误,而不是实际上在问题中调用s.close函数.但结果证明这不是我的问题.

更新结束

这是我的服务器代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import sys

if __name__ == '__main__':
    # Server connection
    s = socket.socket()          # Create a socket object
    host = socket.gethostname()  # Get local machine name
    port = 12345                 # Reserve a port for your service.

    print 'Server started!'
    print 'Waiting for clients...'

    s.bind((host, port))        # Bind to the port
    s.listen(5)                 # Now wait for client connection.
    c, addr = s.accept()     # Establish connection with client.
    print …
Run Code Online (Sandbox Code Playgroud)

python sockets

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

解析电子邮件并从正文中获取号码

我想提取电子邮件正文中找到的第一个数字.在电子邮件库的帮助下,我将邮件中的正文提取为字符串.但问题是,在真正的纯文本体开始之前,有一些关于编码的信息(这些包含数字).如何以可靠的方式跳过那些不依赖于创建电子邮件的客户端而只是第一个数字的客户端.

如果我做了

match = re.search('\d+', string, re.MULTILINE)
Run Code Online (Sandbox Code Playgroud)

它将获得有关编码或其他信息的第一个匹配,而不是实际的邮件内容.

好.我添加了一个样本.这就是它的外观(我将提取123).但我想从另一个客户端发送它可能看起来不同.

--14dae93404410f62f404b2e65e10
Content-Type: text/plain; charset=ISO-8859-1

Junk 123 Junk

--14dae93404410f62f404b2e65e10
Content-Type: text/html; charset=ISO-8859-1

<p>Junk 123 Junk</p>

--14dae93404410f62f404b2e65e10--

更新: 现在我坚持使用迭代器: - /我真的尝试过.但我不明白.这段代码:

msg = email.message_from_string(raw_message)
for part in email.iterators.typed_subpart_iterator(msg, 'text', 'plain'):
    print part
Run Code Online (Sandbox Code Playgroud)

输出:

--14dae93404410f62f404b2e65e10
Content-Type: text/plain; charset=ISO-8859-1

Junk 123 Junk

--14dae93404410f62f404b2e65e10
Content-Type: text/html; charset=ISO-8859-1

<p>Junk 123 Junk</p>

--14dae93404410f62f404b2e65e10--
Run Code Online (Sandbox Code Playgroud)

为什么不输出:

Junk 123 Junk
Run Code Online (Sandbox Code Playgroud)

python regex email

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

使用TLS的python smtp sendmail - 失败

我写了一个python脚本来发送邮件和代码如下:

smtp = smtplib.SMTP(MYMAILSERVER, '587')
try:
    smtp.set_debuglevel(1)
    smtp.ehlo()
    if smtp.has_extn('STARTTLS'):
        smtp.starttls()
        smtp.ehlo()
        smtp.login(MYLOGINNAME, PASSWORD)
        smtp.sendmail(FROM, TO, CONTENT)
finally:
    smtp.quit()
Run Code Online (Sandbox Code Playgroud)

我得到的消息如下:

......
data: (354, 'Start mail input; end with <CRLF>.<CRLF>')
send: 'From: xxxx/r/nTo: yyy/r/nSubject: this is a email from tutong/r/n/r/nJust for test and pls ignore it!~_~\r\n.\r\n'
reply: '550 5.7.1 Client does not have permissions to send as this sender\r\n'
reply: retcode (550); Msg: 5.7.1 Client does not have permissions to send as this sender
data: (550, '5.7.1 Client does not have …
Run Code Online (Sandbox Code Playgroud)

python smtp

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

avconv与子进程

似乎我从未真正设法绕过子进程.这个命令行在bash中工作

avconv -i "concat:/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment0.ts|/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment1.ts" -vcodec copy -acodec copy /tmp/test1.ts
Run Code Online (Sandbox Code Playgroud)

如果我试试这个:

cmdline = ['avconv', '-i "concat:/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment0.ts|/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment1.ts"', '-vcodec copy', '-acodec copy', '/tmp/test1.ts']
subprocess.call(cmdline)
Run Code Online (Sandbox Code Playgroud)

它退出avconv时出现以下错误:

avconv version 0.8.3-4:0.8.3-0ubuntu0.12.04.1, Copyright (c) 2000-2012 the Libav developers
  built on Jun 12 2012 16:52:09 with gcc 4.6.3
Unrecognized option 'i "concat:/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment0.ts|/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment1.ts"'
Failed to set value '-vcodec copy' for option 'i "concat:/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment0.ts|/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment1.ts"'
1
Run Code Online (Sandbox Code Playgroud)

我尝试过一些变种(包括shell = True),但我无法弄清楚问题.

更新

Cjb做出了真正有效的答案.但我的真实代码更复杂.我相信很难从中得到一些东西.但我把它丢进去,以防万一我错过了一个obvoius问题.

def run_avconv(output_dir, fnames):
    full_fnames = [os.path.join(output_dir, fname.replace('\n', ''))
                   for fname in fnames]
    concatted_files = '|'.join(full_fnames)
    cmd_line = [AVCONV_CMD,
                '-i',
                '"concat:' …
Run Code Online (Sandbox Code Playgroud)

python subprocess ffmpeg

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

来自python的Linux命令无法完成

如果我adduser testuser从终端运行,命令会问我一些问题,比如密码.但是这段代码:

import os
a = os.system('useradd testuser')
Run Code Online (Sandbox Code Playgroud)

存在错误代码0(没有问题).但它没有问任何问题.这是为什么?我该如何解决它呢?我也试过subprocess做了同样的事情.

python os.system

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

Django无法获取我的应用程序的根URL

我已经制作了一个"应用程序",它应该是我项目的首页,但是当我尝试将它作为我的项目的根目录时,http://127.0.0.1:8000/它会给我一个404.当我把它作为子文件夹时,http://127.0.0.1:8000/frontpage/它可以工作.这是我的urls.py文件:

项目/ urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin admin.autodiscover()

urlpatterns = patterns('',
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    url(r'^/', include('frontpage.urls')),
    url(r'^s/', include('django.contrib.flatpages.urls')),
    url(r'^kalender/', include('events.urls')),
    #url(r'^kontakt/',    include('envelope.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^adminfiles/', include('adminfiles.urls')), )
Run Code Online (Sandbox Code Playgroud)

头版/ urls.py

from django.conf.urls import patterns, url

from frontpage import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
)
Run Code Online (Sandbox Code Playgroud)

python django

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

Simultaneus线程

好.也许你因为没有表现出我尝试过的东西而生我的气.但我真的很困惑线程.同时运行三个线程并在它们全部运行完第四个线程之后最简单的可行方法是什么?这将在一个wx应用程序内部运行,所以我想要一种不会锁定我的程序的方法.

编辑:嗯,第四个不需要是一个线程,但它是一个需要在所有线程完成后运行的方法

python multithreading

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

向量中的擦除功能

我正在努力学习c ++.没有delSong函数,这段代码编译得很好.但有了它,它将无法编译.我无法弄清楚我做错了什么...: - /

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Jukebox {

public:
  void addSong(string artist, string title, string filename) {
    songCounter++;
    song s {songCounter, artist, title, filename};
    Songs.push_back(s);
  }

  void delSong(int pos) {
    Songs.erase(pos);
  }

  void printSong(int pos) {
    cout << Songs[pos].no << ". ";
    cout << Songs[pos].artist << " - ";
    cout << Songs[pos].title << " : ";
    cout << Songs[pos].filename << endl;
  }

  void printSongs() {
    for (int i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

c++ vector

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

标签 统计

python ×7

c++ ×1

django ×1

email ×1

ffmpeg ×1

multithreading ×1

os.system ×1

regex ×1

smtp ×1

sockets ×1

subprocess ×1

vector ×1