python訪問zk(Python訪問字符串中的值)

本文目錄一覽:

如何使用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存入緩存的數據。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/236884.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:02
下一篇 2024-12-12 12:02

相關推薦

  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29

發表回復

登錄後才能評論