sock_test.py :  » Web-Frameworks » Karrigell » Karrigell-3.1 » karrigell » test » 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 » Web Frameworks » Karrigell 
Karrigell » Karrigell 3.1 » karrigell » test » sock_test.py
import os
import sys
import thread
import socket
import urllib

# ugly hacks to make "import Karrigell" work
this_dir = os.path.dirname(__file__)
server_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.append(server_dir)
os.chdir(server_dir)
sys.argv[0] = os.path.join(server_dir,"Karrigell.py")
sys.argv.append(server_dir)

# aaah !
import Karrigell

config = Karrigell.k_config.config[None]
PORT = 8088
server = Karrigell.ThreadingServer('',PORT,Karrigell.handler)
thread.start_new_thread(server.run,())

class Response:

    def __init__(self,s):
        res = ''
        while True:
            buff = s.recv(1024)
            if not buff:
                break
            res += buff
        self.resp_line,self.headers,self.body = self.parse_response(res)

    def parse_response(self,resp):
        lines = resp.split('\n')
        resp_line = lines[0].rstrip()
        if len(lines)==1:
            return resp_line,None,None
        headers = []
        for i,line in enumerate(lines[1:]):
            if line.strip():
                headers.append(line.rstrip())
            else:
                break
        body = '\n'.join(lines[i+2:])
        return resp_line,headers,body

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
while True:
    try:
        s.connect(('localhost',PORT))
        break
    except:
        pass
s.close()

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('localhost',PORT))

s.send('UNKNOWN bla bla\r\n\r\n')
res = Response(s)

print 'BAD REQUEST'

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('localhost',PORT))
s.send('HEAD / HTTP/1.1\r\n')
s.send('\r\n')
res = Response(s) 

print 'HEAD /',res.resp_line

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('localhost',PORT))
s.send('GET /index.html HTTP/1.1\r\n')
s.send('\r\n')
res = Response(s)
print res.body == file(os.path.join(config.root_dir,'index.py'),'rb').read()
print 'GET /index.ks',res.resp_line

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('localhost',PORT))
s.send('GET /demo/calendar/delete.gif HTTP/1.1\r\n')
s.send('\r\n')
res = Response(s)
print res.headers
print 'GET /demo/calendar/delete.gif',res.resp_line

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('localhost',PORT))
s.send('HEAD /demo/calendar/delete.gif HTTP/1.1\r\n')
s.send('\r\n')
res = Response(s)
print 'HEAD /demo/calendar/delete.gif',res.resp_line
print 'headers',res.headers

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('localhost',PORT))
s.send('HEAD /index.html HTTP/1.1\r\n')
s.send('\r\n')
res = Response(s)
print 'HEAD /index.html',res.resp_line

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('localhost',PORT))
s.send('HEAD /index1.html HTTP/1.1\r\n')
s.send('\r\n')
res = Response(s)
print 'HEAD /index1.html',res.resp_line

res = urllib.urlopen('http://localhost:%s/index.html' %PORT)
print len(res.read())

body = urllib.urlencode({'spam':'lqhglg'})
res = urllib.urlopen('http://localhost:%s/demo/myScript.py' %PORT,body)
print res.info()

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('localhost',PORT))
s.send('HEAD /index.html HTTP/1.1\r\n')
s.send('\r\n')
res = Response(s) 
print 'HEAD /'
print res.resp_line
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.