Zum Inhalt springen

Python Scripte: Unterschied zwischen den Versionen

Aus Computech CMS Wiki
Simon Zeberli (Diskussion | Beiträge)
Die Seite wurde neu angelegt: „=MySQL Verbindung= <syntaxhighlight lang="python" line start="1" highlight="5" enclose="div"> #!/usr/bin/python import MySQLdb import sys #db = MySQLdb.connect…“
 
Simon Zeberli (Diskussion | Beiträge)
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
=MySQL Verbindung=
==MySQL Verbindung==
<syntaxhighlight lang="python" line start="1" highlight="5" enclose="div">
<syntaxhighlight lang="python" line start="1" highlight="5" enclose="div">
#!/usr/bin/python
#!/usr/bin/python
Zeile 24: Zeile 24:
for z in result:
for z in result:
print z[1]+': '+z[2]
print z[1]+': '+z[2]
</syntaxhighlight>
==Pushover mit Python==
<syntaxhighlight lang="python" line start="1" highlight="5" enclose="div">
import httplib, urllib
# Application specific variables
application_token = "aDhLdifpd6aTkuuPjLAjYS19hYtQPU"
user_token = "uqsV4ZLmM8q7FjgtbjzyXrUZHheLdx"
# Message specific variables
title = "Position"
message = "Wir sind in"
url = "http://michaelhleonard.com/blog"
# To remove any of these fields you will need to remove them
# from the conn.request() below
# Start your connection with the Pushover API server
conn = httplib.HTTPSConnection("api.pushover.net:443")
# Send a POST request in urlencoded json
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": application_token,
"user": user_token,
"title": title,
"message": message,
"url": url,
}), { "Content-type": "application/x-www-form-urlencoded" })
# Listen for any error messages or other responses
conn.getresponse()
</syntaxhighlight>
</syntaxhighlight>

Version vom 28. Januar 2014, 16:33 Uhr

MySQL Verbindung

#!/usr/bin/python
import MySQLdb
import sys
#db = MySQLdb.connect("194.126.200.54","zeberlie_raspber","Xe-2UY*7Me?8G","zeberlie_raspberry" )

try:
	db = MySQLdb.connect(
		host = "194.126.200.54",
		user = "zeberlie_raspber",
		passwd = "Xe-2UY*7Me?8G",
		db = "zeberlie_raspberry"
		)
except Exception as e:
	sys.exit('keine verbindung zur Datenbank')

cursor = db.cursor()
cursor.execute('INSERT INTO markers (name, address, lat, lng, type) VALUES ("Heldswil","hoehenzelg", "lat","lng","type")')
cursor.execute('SELECT * FROM markers')
result = cursor.fetchall()

if result:
	for z in result:
		print z[1]+': '+z[2]

Pushover mit Python

import httplib, urllib

# Application specific variables
application_token = "aDhLdifpd6aTkuuPjLAjYS19hYtQPU"
user_token = "uqsV4ZLmM8q7FjgtbjzyXrUZHheLdx"

# Message specific variables
title = "Position"
message = "Wir sind in"
url = "http://michaelhleonard.com/blog"
# To remove any of these fields you will need to remove them
# from the conn.request() below

# Start your connection with the Pushover API server
conn = httplib.HTTPSConnection("api.pushover.net:443")

# Send a POST request in urlencoded json
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": application_token,
"user": user_token,
"title": title,
"message": message,
"url": url,
}), { "Content-type": "application/x-www-form-urlencoded" })
# Listen for any error messages or other responses
conn.getresponse()