當前位置:首頁 » 編程語言 » python連接es

python連接es

發布時間: 2022-08-17 20:41:00

1. 如何使用python3.4連接Mysql

序號 描述

1 去github上下載pymysql的安裝包pymysql

2 解壓到某個盤符下

3 打開cmd窗口(win環境下),進入pymysql的根目錄下執行命令,python setup.py install

4 在程序里,導入pymysql

5 開始連接資料庫


資料庫操作的API文檔連接:http://legacy.python.org/dev/peps/pep-0249/

代碼如下:

Python代碼

  • __author__='qindongliang'

  • #導入pymysql的包

  • importpymysql

  • try:

  • #獲取一個資料庫連接,注意如果是UTF-8類型的,需要制定資料庫

  • conn=pymysql.connect(host='localhost',user='root',passwd='qin',db='person',port=3306,charset='utf8')

  • cur=conn.cursor()#獲取一個游標

  • cur.execute('select*fromperson')

  • data=cur.fetchall()

  • fordindata:

  • #注意int類型需要使用str函數轉義

  • print("ID:"+str(d[0])+'名字:'+d[1]+"性別:"+d[2])

  • cur.close()#關閉游標

  • conn.close()#釋放資料庫資源

  • exceptException:print("發生異常")


  • 結果如下:

  • Java代碼

  • D:pythonpython.exeD:/pythonide/pythonprojectworkspace/python/mysql.py

  • ID:1名字:秦天性別:男

  • ID:2名字:王晶性別:女

  • Processfinishedwithexitcode0

  • 原文http://qindongliang1922.iteye.com/blog/2096512

    補充說明:

    mysqldb作為python連接mysql資料庫的工具,但是mysqldb目前支撐的版本較低,安裝失敗。所以才嘗試pymysql,這個比較簡單易用

    軟體下載地址:

    python3.2.5:https://www.python.org/ftp/python/3.2.5/python-3.2.5.msi

    pymysql3.0.5:https://pypi.python.org/packages/source/P/PyMySQL3/PyMySQL3-0.5.tar.gz

    mysql:http://www.phpstudy.net/phpstudy/phpStudyLite.zip(為了方便安裝,我這里選擇phpstudy)

    1、python安裝目錄設定為d:/python32

    2、pymysql安裝方法為:解壓下載的文件,在cmd中運行: python setup.py install。

    檢驗安裝安裝是否成功的方法:import pymysql 。 如果不報錯 說明安裝成功。

    3、mysql安裝目錄為D:/phpStudy/MySQL。為避免更多配置問題,可在啟動phpstudy後,將其設為系統服務

    4、基本操作:

    (1)導入pymysql: import pymysql

    (2)連接資料庫: conn=pymysql.connect(host='localhost',user='root',passwd='root',db='ere',charset='utf8') 務必注意各等號前面的內容!charset參數可避免中文亂碼

    (3)獲取操作游標:cur=conn.cursor()

    (4)執行sql語句,插入記錄:sta=cur.execute("insert 語句") 執行成功後sta值為1。更新、刪除語句與此類似。

    (5)執行sql語句,查詢記錄:cur.execute("select語句") 執行成功後cur變數中保存了查詢結果記錄集,然後再用循環列印結果:

    for each in cur:

    print(each[1].decode('utf-8')) # each[1] 表示當前游標所在行的的第2列值,如果是中文則需要處理編碼

2. 如何列印python 的es有幾種search

輕量級:安裝啟動方便,下載文件之後一條命令就可以啟動;Schemafree:可以向伺服器提交任意結構的JSON對象,Solr中使用schema.xml指定了索引結構;多索引文件支持:使用不同的index參數就能創建另一個索引文件,Solr中需要另行配置;分布式:SolrCloud的配置比較復雜。

3. python3 怎麼連接資料庫

Python是計算機常用的計算機語言,在實際的操作中我們會涉及到Python連接資料庫的相關實際操作,假如你對Python連接資料庫的實際操作方案感興趣或是有疑問,你都可以瀏覽下面的文章。一. Python和mysql資料庫連接Python 要連接 MySQL 可以使用 MySQL_python模塊首先確定是否安裝,在指令模式輸入 python,然後便可以開始檢查:Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 Type "help", "right", "credits" or "license"
for more information. >>> import MySQLdb Traceback (most recent call last): File "", line 1, in ImportError: No mole named MySQLdb >>> exit() 如果見以上面的"ImportError: No mole named MySQLdb" 一句,便表示系統沒有安裝,注意:在shell中,輸出是 區分大小寫的也可以通過輸入下面這些命令來測試你的資料庫配置:>>> from django.db import connection >>>
cursor = connection.cursor() 如果沒有顯示什麼錯誤信息,那麼你的資料庫配置是正確的。 否則,你就得查看錯誤信息來糾正錯誤。上面的相關代碼是對Python連接資料庫中Python和mysql資料庫連接的前部分代碼的示例。 安裝mysql_python模塊到MySQLdb 官方網站 下載並安裝MySQLdb版本:(win)MySQL-python-1.2.2.win32-py2.6.exe 直接運行安裝即可按如下步驟安裝$ tar zxvf MySQL-python-1.2.2.tar.gz $ cd MySQL-python-1.2.2 $ python setup.py build $ python setup.py install 附件: libguide40.dll.zip(77.3 KB) libmmd.dll.zip(169 KB) libmySQL.dll.zip(861 KB) 以上就是對python連接資料庫中 python和mysql資料庫連接相關的內容的介紹,望採納

4. 如何用python 連接兩個資料庫

在 Python 語言環境下我們這樣連接資料庫。

In [1]: from mysql import connector

In [2]: cnx = connector.connect(host="172.16.192.100",port=3306,user="appuser",password="xxxxxx")

但是連接資料庫的背後發生了什麼呢?


答案

當我們通過驅動程序(mysql-connector-python,pymysql)連接 MySQL 服務端的時候,就是把連接參數傳遞給驅動程序,驅動程序再根據參數會發起到 MySQL 服務端的 TCP 連接。當 TCP 連接建立之後驅動程序與服務端之間會按特定的格式和次序交換數據包,數據包的格式和發送次序由MySQL 協議規定。MySQL 協議:https://dev.mysql.com/doc/internals/en/client-server-protocol.html整個連接的過程中 MySQL 服務端與驅動程序之間,按如下的次序發送了這些包。

  • MySQL 服務端向客戶端發送一個握手包,包里記錄了 MySQL-Server 的版本,默認的授權插件,密碼鹽值(auth-data)。

  • 2. MySQL 客戶端發出 ssl 連接請求包(如果有必要的話)。

    3. MySQL 客戶端發出握手包的響應包,這個包時記錄了用戶名,密碼加密後的串,客戶端屬性,等等其它信息。

    4. MySQL 服務端發出響應包,這個包里記錄了登錄是否成功,如果沒有成功也會給出錯誤信息。

5. python elasticsearch 怎麼用

標准語法
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()

doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])

res = es.get(index="test-index", doc_type='tweet', id=1)
print(res['_source'])

es.indices.refresh(index="test-index")

res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

可以參考官方文檔
https://elasticsearch-py.readthedocs.io/en/master/

6. python怎麼連接websocket

以下有一個例子,是基於python27版本的,先要pip安裝websocket-client。

大概流程如下,具體的傳輸的數據,還是要知道client和server之間的消息通訊規定,改成自己需要的

#-*-encoding:utf-8-*-

importsys
fromsocketimport*
importjson,time,threading
fromwebsocketimportcreate_connection
reload(sys)
sys.setdefaultencoding("utf8")

#config={
#'HOST':'127.0.0.1',
#'PORT':10086
#}
#pipinstallwebsocket-client

classClient():
def__init__(self):
#調用create_connection方法,建立一個websocket鏈接
#鏈接地址請修改成你自己需要的
self.ws=create_connection("ws://47.93.91.89:10086/name/hehe")
#建一個線程,監聽伺服器發送給客戶端的數據
self.trecv=threading.Thread(target=self.recv)
self.trecv.start()

#發送方法,聊天輸入語句時調用,此處默認為群聊ALL
defsend(self,content):
#這里定義的消息體要換成你自己的消息體,變成你需要的。
msg={
"type":"POST",
"username":"hehe",
"sendto":"ALL",
"content":content

}
msg=json.mps(msg)
self.ws.send(msg)

#接收服務端發送給客戶的數據,只要ws處於連接狀態,則一直接收數據
defrecv(self):
try:
whileself.ws.connected:
result=self.ws.recv()
print"receivedmsg:"+str(result)
exceptException,e:
pass


#關閉時,發送QUIT方法,退出ws鏈接
defclose(self):
#具體要知道你自己退出鏈接的消息體是什麼,如果沒有,可以不寫這個方法
msg={
"type":"QUIT",
"username":"johanna",
"content":"byebye,everyone"
}
msg=json.mps(msg)
self.ws.send(msg)


if__name__=='__main__':

c=Client()
#當輸入非exit時,則持續ws鏈接狀態,如果exit,則關閉鏈接
whileTrue:
content=raw_input("pleaseinput(inputexittoexit):")
ifcontent=="exit":
c.close()
break
else:
c.send(content)
time.sleep(1)

7. python怎麼定義elasticsearch的類型

1

curl -X POST -d '{"title":"jones","amount":5.7}'

但是聽說,1.x之後不能直接curl,這不是重點忽略
下面介紹一個python使用elasticsearch的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

from datetime import datetime
from elasticsearch import Elasticsearch

#連接elasticsearch,默認是9200
es = Elasticsearch()

#創建索引,索引的名字是my-index,如果已經存在了,就返回個400,
#這個索引可以現在創建,也可以在後面插入數據的時候再臨時創建
es.indices.create(index='my-index',ignore)
#{u'acknowledged':True}

熱點內容
央視影音緩存視頻怎麼下載視頻 發布:2024-04-27 00:25:55 瀏覽:583
手機緩存的視頻怎麼看 發布:2024-04-27 00:11:05 瀏覽:57
shell腳本平方計算公式 發布:2024-04-26 23:29:26 瀏覽:187
比較實惠的雲伺服器 發布:2024-04-26 23:24:57 瀏覽:974
怎麼增加電腦緩存 發布:2024-04-26 23:23:46 瀏覽:451
android調試gdb 發布:2024-04-26 23:22:27 瀏覽:99
androidsocket服務 發布:2024-04-26 22:49:53 瀏覽:980
python編譯時加密 發布:2024-04-26 22:49:20 瀏覽:246
買車看哪些配置參數 發布:2024-04-26 22:45:50 瀏覽:835
linux顯示圖像 發布:2024-04-26 22:45:41 瀏覽:493