Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library: Python 1.0
Community Wiki and Resources
Download Client Library
Python Client Library
SDK Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
2 Tutorial
Chapter Sections
Chapters

2.10. Bringing It All Together

Almost done! The above steps were the hard part, next comes the easy part, putting the data into the database. We import the part of the couchbase module we need, issue a couple .set() functions, then instead of printing the source strings, we request the data from the database using a .get() call and print it to confirm data was entered.

Note that you must issue the closing cb.done() call or your script will hang. The Couchbase module uses multithreading and this helps it tie up any loose ends. Should you forget to do this call you will likely have to kill the python process in your task/activity manager (Windows/OSX) or by issuing the kill command (Linux/OSX) for the Python process id (pid) as reported by the ps -e command.

import nmap, json, time

from couchbase.couchbaseclient import VBucketAwareCouchbaseClient as CbClient
cb = CbClient("http://localhost:8091/pools/default","default","",False)

def scan(host='127.0.0.1',range='22-80'):
  '''Setup port scanner'''
  nm = nmap.PortScanner()
  nm.scan(host, range)
  return nm

nm = scan()

for host in nm.all_hosts():
  host_state = {}
  host_state["host"] = host
  host_state["state"] = nm[host].state()
  for proto in nm[host].all_protocols():
    for port in nm[host][proto].keys():
      port_state = nm[host][proto][port]
      port_state["host"] = host
      port_state["protocol"] = proto
      port_state["port"] = port
      timestamp = time.time()
      host_state["timestamp"] = str(timestamp)
      port_state["timestamp"] = str(timestamp)
      docid = str(host) + ":" + str(proto) + ":" + str(port) + ":" + str(port_state["timestamp"])
      cb.set(docid,0,0,json.dumps(port_state))
      cb.set(str(host),0,0,json.dumps(host_state))
      print cb.get(docid)
      print cb.get(str(host))

cb.done()

You can download the script, including more detailed comments, from: nmap-tutorial-complete.py