Facebook
From aaa, 4 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 210
  1. #!/usr/bin/env python3
  2. import time
  3. import socket
  4. import json
  5. import os
  6. import syslog
  7. from multiprocessing import Queue, Process
  8. import pypledge
  9.  
  10. def ValidateInput(q):
  11.         syslog.syslog(syslog.LOG_INFO, 'Validation Started')
  12.         pypledge.pledge([ 'stdio'])
  13.         q.get()
  14.         print(q)
  15.         print(type(q))
  16.         message = input.decode()
  17.         try:
  18.                 messageJson = json.dumps(message)
  19.         except Exception:
  20.                 syslog.syslog(syslog.LOG_ERR, 'Error in Json Parsing')
  21.  
  22.         syslog.syslog(syslog.LOG_INFO, time.gmtime() + messageJson)
  23.  
  24.  
  25. def CoreServer(q):
  26.         syslog.syslog(syslog.LOG_INFO,'Server Started')
  27.         pypledge.pledge(['inet', 'stdio'])
  28.  
  29.         HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
  30.         PORT = 65432        # Port to listen on (non-privileged ports are > 1023)
  31.        
  32.         run_flag = true
  33.        
  34.        
  35.         with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: # ipv4 i tcp
  36.                 s.setblocking(0)
  37.                 s.bind((HOST, PORT))
  38.                 s.listen()
  39.                 conn, addr = s.accept()
  40.                 print(conn)
  41.                 with conn:
  42.                         while True:
  43.                                 while True:
  44.                                         try:
  45.                                                 data = conn.recv(1024)
  46.                                                
  47.                                         except socket.error, e:
  48.                                                 err = e.args[0]
  49.                                                 if err == errno.EWOULDBLOCK:
  50.                                                         continue
  51.                                         break
  52.                                
  53.                                 q.put(data)
  54.                                 syslog.syslog(syslog.LOG_INFO,q)
  55.                                 if not data:
  56.                                         break
  57.                                 conn.sendall(data)
  58.                 s.close()        
  59.            
  60.  
  61. def RunningServer():
  62.         syslog.syslog(syslog.LOG_INFO,  'Main Process is Started')     
  63.         pypledge.pledge(['proc', 'stdio'])
  64.        
  65.         queue = Queue()
  66.         print("Process id before forking: {}".format(os.getpid()))
  67.         core = Process(target=CoreServer, args=(queue,))
  68.         valid = Process(target=ValidateInput, args=(queue,))
  69.         core.start()
  70.         valid.start()
  71.    
  72.  
  73. RunningServer()
  74.