def main(argv): # Init parser obj core = Parser() # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = ('', TCP_PORT) print >> sys.stderr, '(TCPServer.py) starting up on %s port %s' % server_address sock.bind(('', TCP_PORT)) # Listen for incoming connections sock.listen(1) ans = '' while True: # Wait for a connection print >> sys.stderr, '(TCPServer.py) waiting for a connection' connection, client_address = sock.accept() try: print >>sys.stderr, '(TCPServer.py) connection from', client_address # Receive the data in small chunks and retransmit it while True: incoming_data = connection.recv(1024) print >>sys.stderr, '(TCPServer.py) received string from tcp client "%s"' % incoming_data # se ci sono dati li processo if incoming_data: try: # chiamo il parser json ans = core.processCommand(incoming_data) except (ValueError, KeyError, TypeError): print >> sys.stderr, "(TCPServer.py) JSON format error" print '(TCPServer.py) sending data back to the client' connection.sendall(ans) else: print >>sys.stderr, '(TCPServer.py) no more data from', client_address break except KeyboardInterrupt: connection.close() print "(TCPServer.py) Interruzione da tastiera gestita" finally: # Clean up the connection connection.close()
def main(): scrapper = Scrapper() merger = Merger() parser = Parser() client = MongoClient('localhost', 27017) db = client['Data'] collection_socialmedia = db['socialmedia'] #Begin real time collecting while True: scrapper.scrap() merger.main() parser.main() sleep(3600) #Storing to mangoDB f = open( '/home/sartharion/Bureau/stage/POO/data.json', 'r') file_data = json.load(f) collection_socialmedia.delete_many({}) collection_socialmedia.insert_many(file_data) client.close()
import socket import sys import json import traceback from JsonParser import Parser # Init parser obj core = Parser() # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = ('', 10000) print >> sys.stderr, '(TCPServer.py) starting up on %s port %s' % server_address sock.bind(('', 10000)) # Listen for incoming connections sock.listen(1) ans = '' while True: # Wait for a connection print >> sys.stderr, '(TCPServer.py) waiting for a connection' connection, client_address = sock.accept() try: print >> sys.stderr, '(TCPServer.py) connection from', client_address # Receive the data in small chunks and retransmit it