首页 > 编程知识 正文

python访问zk(Python访问字符串中的值)

时间:2023-12-15 19:09:12 阅读:316173 作者:ABXC

本文目录一览:

如何使用python脚本调用zk 命令

zk命令是你系统中的命令吗

如果是的话

import os

os.system(zk)

这样就可以调用啦

如何利用pykafka远程消费 zookeeper+kafka集群 python脚本

#从kafka消费

#consumer_area = topic_area.get_simple_consumer(auto_offset_reset=OffsetType.LATEST)

#从ZOOKEEPER消费

consumer_area = topic_area.get_balanced_consumer(

consumer_group=b'zs_download_04', # 自己命令

auto_offset_reset=OffsetType.LATEST,#在consumer_group存在的情况下,设置此变量,表示从最新的开始取

#auto_offset_reset=OffsetType.EARLIEST,

#reset_offset_on_start=True,

auto_commit_enable=True,

#auto_commit_interval_ms=1,

zookeeper_connect=ZK_LIST

)

python 多线程 访问网站

#python2

#coding=utf-8

import os,re,requests,sys,time,threading

reload(sys)

sys.setdefaultencoding('utf-8')

class Archives(object):

    def __init__(self, url):

        self.url = url

    

    def save_html(self, text):

        fn = '{}_{}'.format(int(time.time()), self.url.split('/')[-1])

        dirname = 'htmls'

        if not os.path.exists(dirname):

            os.mkdir(dirname)

        with open(os.path.join(dirname, fn), 'w') as f:

            f.write(text)

            

    def get_htmls(self):

        try:              

            r =  requests.get(self.url)

            r.raise_for_status()

            r.encoding = r.apparent_encoding

            print 'get html from ', url

            self.save_html(r.text)

        except Exception,e:

            print '爬取失败',e            

    def main(self):

        thread = threading.Thread(target=self.get_htmls())

        thread.start()

        thread.join()

if __name__=='__main__':

    start=time.time()

    fn = sys.argv[1] if len(sys.argv)1 else 'urls.txt'

    with open(fn) as f:

        s = f.readlines()

    for url in set(s):

        a=Archives(url.strip())

        a.main()    

    end=time.time()

    print end-start

python zookeeper 怎么入参

前提是zookeeper安装包已经在/usr/local/zookeeper下

cd /usr/local/zookeeper/src/c

./configure

make

make install

wget --no-check-certificate

tar -zxvf zkpython-0.4.tar.gz

cd zkpython-0.4

sudo python setup.py install

zkpython应用

下面是网上一个zkpython的类,用的时候只要import进去就行

vim zkclient.py

#!/usr/bin/env python2.7

# -*- coding: UTF-8 -*-

import zookeeper, time, threading

python无法访问手机存储文件夹

无法访问的原因是某一个磁盘中保存了Python的编译环境。在这个编译的环境下,存在一个python.exe文件,然后又对文件换了一个地方存储,所以就会找不到指定的程序文件。解决方法如下:

1、进入到pycharm中,找到菜单栏中的“file”选项,在下拉的列表中选择“settings”,进入到设置对话框。

2、左侧中找到“ProjectInterpreter”选项,在点击它之后,在点击右侧的“设置”图案,在选项中选择“ShowAll”,这样就将全部的项目进行呈现了。

3、打开“ProjectInterpreter”对话框,找到我们旧程序的安装位置,也就是换了位置的文件并选择,在点击右侧的“-”号进行删除。

4、将旧的程序进行删除之后,点右侧的“+”号,这个时候会弹出一个“AddPython…”对话框,找到界面中的“Existiingenvironment”,在右侧的“...”中,添加一个存在的Python环境。

5、添加了之后,点击“OK”,此时在运行文件时,就不会报文件找不到的错误了。

python爬虫,集群是如何实现节点的发现和管理

Ignite集群管理——基于Zookeeper的节点发现

Ignite支持基于组播,静态IP,Zookeeper,JDBC等方式发现节点,本文主要介绍基于Zookeeper的节点发现。

环境准备,两台笔记本电脑A,B。A笔记本上使用VMware虚拟机安装了Ubuntu系统C。

1、 C安装Zookeeper

由于主要测试Ignite,这里仅仅简单安装一个zookeeper节点,下载zookeeper解压后,直接执行zookeeper目录下的bin/zkServer.sh start命令则成功启动zookeeper。

查看Ubuntu系统C的IP地址为192.168.1.104,zookeeper默认端口为12181。

注意:这里VMware虚拟机的网络适配器一定要选择桥接模式,否则A机器无法访问虚拟机。

2、 A系统运行Ignite节点1;

代码中加粗的部分便是Ignite注册代码,十分简单。

package com.coshaho.learn.ignite.cluster;import org.apache.ignite.Ignite;import org.apache.ignite.IgniteCache;import org.apache.ignite.Ignition;import org.apache.ignite.cache.CacheMode;import org.apache.ignite.configuration.CacheConfiguration;import org.apache.ignite.configuration.IgniteConfiguration;import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;import org.apache.ignite.spi.discovery.tcp.ipfinder.zk.TcpDiscoveryZookeeperIpFinder;/**

*

* IgniteCluster01.java Create on 2017年6月3日 下午10:52:38    

*    

* 类功能说明:   基于zookeeper集群

*

* Copyright: Copyright(c) 2013

* Company: COSHAHO

* @Version 1.0

* @Author coshaho */public class IgniteCluster01

{    public static void main(String[] args)

   {        TcpDiscoverySpi spi = new TcpDiscoverySpi();

       TcpDiscoveryZookeeperIpFinder ipFinder = new TcpDiscoveryZookeeperIpFinder();        // Specify ZooKeeper connection string.

       ipFinder.setZkConnectionString("192.168.1.104:12181");

       spi.setIpFinder(ipFinder);

       IgniteConfiguration cfg = new IgniteConfiguration();        // Override default discovery SPI.        cfg.setDiscoverySpi(spi);        // Start Ignite node.

       Ignite ignite =Ignition.start(cfg);

System.out.println("IgniteCluster1 start OK.");

       CacheConfigurationInteger, String cacheCfg = new CacheConfigurationInteger, String();

       cacheCfg.setBackups(1);

       cacheCfg.setCacheMode(CacheMode.PARTITIONED);

       cacheCfg.setName("myCache");

       IgniteCacheInteger, String cache = ignite.getOrCreateCache(cacheCfg);

       cache.put(1, "ignite1");

       System.out.println(cache.get(1));

       System.out.println(cache.get(2));

   }

}

3、 B系统运行Ignite节点2;

package com.coshaho.learn.ignite.cluster;import org.apache.ignite.Ignite;import org.apache.ignite.IgniteCache;import org.apache.ignite.Ignition;import org.apache.ignite.cache.CacheMode;import org.apache.ignite.configuration.CacheConfiguration;import org.apache.ignite.configuration.IgniteConfiguration;import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;import org.apache.ignite.spi.discovery.tcp.ipfinder.zk.TcpDiscoveryZookeeperIpFinder;public class IgniteCluster02

{    public static void main(String[] args)

{

TcpDiscoverySpi spi = new TcpDiscoverySpi();

TcpDiscoveryZookeeperIpFinder ipFinder = new TcpDiscoveryZookeeperIpFinder();        // Specify ZooKeeper connection string.

ipFinder.setZkConnectionString("192.168.1.104:12181");

spi.setIpFinder(ipFinder);

IgniteConfiguration cfg = new IgniteConfiguration();        // Override default discovery SPI.        cfg.setDiscoverySpi(spi);        // Start Ignite node.

Ignite ignite =Ignition.start(cfg);

System.out.println("IgniteCluster2 start OK.");

CacheConfigurationInteger, String cacheCfg = new CacheConfigurationInteger, String();

cacheCfg.setBackups(1);

cacheCfg.setCacheMode(CacheMode.PARTITIONED);

cacheCfg.setName("myCache");

IgniteCacheInteger, String cache = ignite.getOrCreateCache(cacheCfg);

cache.put(2, "ignite2");

System.out.println(cache.get(1));

System.out.println(cache.get(2));

}

}

可以看到,Ignite节点2可以成功访问到Ignite节点1存入缓存的数据。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。