小编kri*_*itx的帖子

Python从readlines读取前四行()

我如何阅读前四行readlines(),我STDIN从代理到我的脚本:

GET http://www.yum.com/ HTTP/1.1
Host: www.yum.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Proxy-Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)

我使用它sys.stdin.readlines()并将其记录到文件,但我想只记录GETUser-Agent行到文件.

while True:
    line = sys.stdin.readlines()
    for l in line:
        log = open('/tmp/redirect.log', 'a')
        log.write(l)
        log.close()
Run Code Online (Sandbox Code Playgroud)

python

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

Cython性能测试

我试图将我的python代码移植到C,但在此之前我做了性能测试,但看起来它没有提高性能.

首先是C程序:

#include <stdio.h>
main ()
{
    printf("hello world \n");
}

[root@server c]$ gcc test.c
[root@server c]$ time ./a.out
hello world

real    0m0.001s
user    0m0.000s
sys     0m0.000s
Run Code Online (Sandbox Code Playgroud)

第二个Python程序:

#!/usr/bin/env python

print "hello world \n"


[root@server c]$ time python test.py
hello world


real    0m0.024s
user    0m0.020s
sys     0m0.003s
Run Code Online (Sandbox Code Playgroud)

第三次Cython ......

test.py

print "hello world \n"

[root@server c]$ cython --embed test.py
[root@server c]$ gcc $CFLAGS -I/usr/include/python2.6 -o test test.c -lpython2.6 -lpthread -lm -lutil -ldl

[root@server c]$ time ./test
hello world


real    0m0.024s …
Run Code Online (Sandbox Code Playgroud)

python linux cython

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

C创建具有给定大小的文件

我试图使用lseek()创建一个给定大小的文件,并在文件的末尾添加一个字节,但它创建一个0字节的稀疏文件.

以下是代码...任何建议?

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#ifndef BUF_SIZE
#define BUF_SIZE 1024
#endif // BUF_SIZE

int main(int argc, char *argv[])
{
    int inputFd;
    int fileSize = 500000000;
    int openFlags;
    int result;

    mode_t filePerms;
    ssize_t numRead;
    char buf[BUF_SIZE];

    openFlags = O_WRONLY | O_CREAT | O_EXCL;
    filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /*rw-rw-ew*/

    inputFd = open(argv[1], openFlags, filePerms);
    if (inputFd == -1)
        printf("problem opening file %s ", argv[1]); …
Run Code Online (Sandbox Code Playgroud)

c c++ linux

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

c ++字符串函数

我正在尝试创建一个可以从主函数调用的字符串函数,但这会给出分段错误...任何想法???

也可以创建一个返回字符串的字符串函数.

#include <iostream>
#include <stdio.h>
#include <sstream>


using namespace std;

string callMe(string& k){

    cout << "String from callMe: " << k;

}


int main(){

   string k;

   k = "K SHK";

   cout << "String from main function: " << k << endl;

   callMe(k);

}
Run Code Online (Sandbox Code Playgroud)

更新:

它编译好..

[root@server dev]# ./stringtest 
String from main function: K SHK
Segmentation fault
Run Code Online (Sandbox Code Playgroud)

c++ linux

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

create_resources - 无法将字符串转换为哈希

我正在使用 elasticsearch-logstash 模块

https://forge.puppetlabs.com/elasticsearch/logstash

这就是我的 hiera 的样子...

---
classes:
  - 'profile::logstash'


profile::logstash::conf:
  package_url: "https://download.elasticsearch.org/logstash/logstash/packages/centos/logstash-contrib-1.4.2-1_efd53ef.noarch.rpm"
Run Code Online (Sandbox Code Playgroud)

这是配置文件中的 logstash.pp

class profile::logstash {

  $conf   = hiera('profile::logstash::conf',{})


  validate_hash($conf)


  create_resources('logstash',$conf)

}
Run Code Online (Sandbox Code Playgroud)

我收到这个错误...

Error: can't convert String into Hash at /tmp/vagrant-puppet-2/modules-0/profile/manifests/logstash.pp:10 on node pw-idx-11.local
Wrapped exception:
can't convert String into Hash
Error: can't convert String into Hash at /tmp/vagrant-puppet-2/modules-0/profile/manifests/logstash.pp:10 on node pw-idx-11.local
Run Code Online (Sandbox Code Playgroud)

更新:

使用这个固定....

class profile::elasticsearch {

  class { '::elasticsearch':
    version => '1.1.1-1'
  }

  $elasticsearch_configs   = hiera_hash('profile::elasticsearch::instance',{})

  validate_hash($elasticsearch_configs)

  create_resources(elasticsearch::instance,$elasticsearch_configs)

  package { 'java-1.7.0-openjdk.x86_64':
    ensure => 'installed'
  }

}
Run Code Online (Sandbox Code Playgroud)

puppet logstash

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

puppet覆盖模块上的文件资源

我正在使用这个CIS模块arildjensen/cis-puppet

并希望覆盖/etc/profileCIS模块上的文件声明

所以我在这篇文章之后创建了这个新的清单

class profile::hadoop::settings inherits cis {

      file { '/etc/profile':
        ensure => 'file',
        owner  => 'root',
        group  => 'root',
        mode   => '0600',
        source => 'puppet:///modules/profile/hadoop/etc/profile',
      }
  }
Run Code Online (Sandbox Code Playgroud)

但是这仍然会给出错误

Error: Duplicate declaration: File[/etc/profile] is already declared in file /tmp/vagrant-puppet/modules-ab9b45e51a68912cdc576c81d46a2260/profile/manifests/hadoop/settings.pp:9; cannot redeclare at /tmp/vagrant-puppet/modules-ab9b45e51a68912cdc576c81d46a2260/cis/manifests/linuxcontrols/c0076.pp:12 on node server.localdomain
Run Code Online (Sandbox Code Playgroud)

linux automation centos puppet

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

php意外T_IS_NOT_EQUAL错误

我正在尝试制作一个接受密码文本的PHP脚本,并将从数据库中删除相关数据.加载脚本时出现此错误

语法错误,第4行/home2/krisindi/public_html/deletead.php中的意外T_IS_NOT_EQUAL

    <?php
$password = $_POST["password"];

if ( $password ) != 0 )
        {
                $id = $data->select ("Classified", "AdID", array ("Password => ($password)));
                $data->delete ( "AdExtraField" , array ( "AdID" => intval ( $id["AdID"] ) ) ) ;
                $data->delete ( "Classified" , array ( "Password" => ( $password ) ) ) ;
                exec ( "chmod ../media/ 777" ) ;

                $image_file = "../media/cls_".$id["AdID"]."_520.jpg" ;
                if ( file_exists ( $image_file ) )
                        unlink ( $image_file ) ;

                for ( $i = 1 …
Run Code Online (Sandbox Code Playgroud)

php mysql

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

python捕获异常

我正在运行curl命令来检查网站的状态:

try:
    connectionTest = subprocess.Popen([r"curl --interface xx.xx.xx.xx http://www.yahoo.com"], shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    cstdout,cstderr = connectionTest.communicate()
    if cstdout:
        #print cstdout
        status = "OK"
    elif cstderr:
        #print cstderr
        status = "PROBLEM"
except:
    e = sys.exc_info()[1]
    print "Error: %s" % e
Run Code Online (Sandbox Code Playgroud)

代码工作正常,除了try:except语句没有正确捕获异常,下面是接口关闭时脚本的输出,现在我想在except语句中捕获第一行...而不是产生......这可能吗?

SIOCSIFFLAGS: Cannot assign requested address

PROBLEM
Run Code Online (Sandbox Code Playgroud)

python linux

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

标签 统计

linux ×5

python ×3

c++ ×2

puppet ×2

automation ×1

c ×1

centos ×1

cython ×1

logstash ×1

mysql ×1

php ×1