webwarelistener2.py :  » Blog » Frog » FrogComplete-1.8 » snakeserver » adapters » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Blog » Frog 
Frog » FrogComplete 1.8 » snakeserver » adapters » webwarelistener2.py
import socket
import time
import select

from marshal import dumps,loads


int_length = len(dumps(int(1)))    #length of a marshalled integer


class WebWareAdapter:
  def __init__(self):
    self.port = 8086
    self.insock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.insock.bind(('',self.port))
    self.insock.listen(1)
    print "******** Listening to WebWare Socket ************"

  def handleRequest(self):

    startTime = time.time()
    print 'BEGIN REQUEST'
    print time.asctime(time.localtime(startTime))
    conn = self.sock
    print 'receiving request from', conn

    BUFSIZE = 8*1024


    chunk = ''
    missing = int_length
    while missing > 0:
      block = conn.recv(missing)
      if not block:
        conn.close()
        raise socket.error, 'received only %d out of %d bytes when receiving dict_length' % (len(chunk), int_length)
      chunk = chunk + block
      missing = int_length - len(chunk)
    dict_length = loads(chunk)
    if type(dict_length) != type(1):
      conn.close()
      print "Error: Invalid AppServer protocol"
      return 0

    chunk = ''
    missing = dict_length
    while missing > 0:
      block = conn.recv(missing)
      if not block:
        conn.close()
        raise socket.error, 'received only %d out of %d bytes when receiving dict' % (len(chunk), dict_length)
      chunk = chunk + block
      missing = dict_length - len(chunk)

    dict = loads(chunk)

    return dict


  def listen(self):
    while 1:
      ins,outs,excs=select.select([self.insock],[],[])
      if self.insock in ins:
        (self.sock,addr )=self.insock.accept()
        print "GOT REQUEST FROM ",addr
        data=self.handleRequest()
        
        self.sock.send("resultaat")
        print "END OF REQUEST"
        try:
          self.sock.shutdown(1)
          self.sock.close()
        except:
          pass

        
w=WebWareAdapter()
w.listen()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.