Environment is TG 1.0.4.4 running on Python 2.5.2 on a Windows XP
Professional host.

I need to use httplib in my TG app to connect to an embedded web
server for some status information, and am seeing different results
when I use the same code standalone or in my TG app.

For example the following code works correctly:

[BEGIN SNIP]
import httplib
import re

try:
    conn = httplib.HTTPConnection('somehost:8000')
    conn.connect()
    conn.request('GET', '/controlplugin?usermonitor&ShowUserCount')
    response = conn.getresponse()
    body = response.read()
    pattern = re.compile('\d+')
    num_users = pattern.findall(body)[0]
    return num_users
except httplib.HTTPException, ex:
    return "unavailable"
[END SNIP]

Running this code works correctly: it parses the response back from
the embedded web server.  The response returned looks like the
following:

<html>
<head/>
<body>
Message:Total Users = 0
<p/>
</body>

Presume for the moment that I have no control over the format over the
results returned.

Now if I take the same code and place it in the context of my TG app I
get the following:

[BEGIN SNIP]
    def get_num_users(self, shard):
        """
        contacts the userentrypoint of the shard and queries for the
num of connected users
        """
        try:
            conn = httplib.HTTPConnection('somehost:8000')
            conn.connect()
            conn.request('GET', '/controlplugin?
usermonitor&ShowUserCount')
            response = conn.getresponse()
            body = response.read()
            conn.close()
            pattern = re.compile('\d+')
            num_users = pattern.findall(body)[0]
            return num_users
        except httplib.HTTPException, ex:
            return 'unavailable'
[END SNIP]

This code is called from a get_shards() method @tg.expose()d from my
controllers.py.  When I run it I observe the following:

2008-04-14 15:38:19,315 holocron.controllers INFO get_shards
connecting
requesting
2008-04-14 15:38:19,361 cherrypy.msg INFO HTTP: Page handler: <bound
method Root.get_shards of <holocron.controllers.Root object at
0x015DCDD0>>
Traceback (most recent call last):
  File "c:\development\tools\python25\lib\site-packages\cherrypy-2.3.0-
py2.5.egg\cherrypy\_cphttptools.py", line 121, in _run
    self.main()
  File "c:\development\tools\python25\lib\site-packages\cherrypy-2.3.0-
py2.5.egg\cherrypy\_cphttptools.py", line 264, in main
    body = page_handler(*virtual_path, **self.params)
  File "<string>", line 3, in get_shards
  File "c:\development\tools\python25\lib\site-packages
\TurboGears-1.0.4.4-py2.5.egg\turbogears\controllers.py", line 365, in
expose
    *args, **kw)
  File "<string>", line 5, in run_with_transaction
  File "c:\development\tools\python25\lib\site-packages
\TurboGears-1.0.4.4-py2.5.egg\turbogears\database.py", line 356, in
so_rwt
    retval = func(*args, **kw)
  File "<string>", line 5, in _expose
  File "c:\development\tools\python25\lib\site-packages
\TurboGears-1.0.4.4-py2.5.egg\turbogears\controllers.py", line 380, in
<lambda>
    mapping, fragment, args, kw)))
  File "c:\development\tools\python25\lib\site-packages
\TurboGears-1.0.4.4-py2.5.egg\turbogears\controllers.py", line 408, in
_execute_func
    output = errorhandling.try_call(func, *args, **kw)
  File "c:\development\tools\python25\lib\site-packages
\TurboGears-1.0.4.4-py2.5.egg\turbogears\errorhandling.py", line 72,
in try_call
    return func(self, *args, **kw)
  File "F:\workspace\Holocron\holocron\controllers.py", line 85, in
get_shards
    shard_json["shardNumUsers"] = self.get_shard_num_users(shard)
  File "F:\workspace\Holocron\holocron\controllers.py", line 67, in
get_shard_num_users
    response = conn.getresponse()
  File "C:\development\tools\Python25\lib\httplib.py", line 928, in
getresponse
    response.begin()
  File "C:\development\tools\Python25\lib\httplib.py", line 385, in
begin
    version, status, reason = self._read_status()
  File "C:\development\tools\Python25\lib\httplib.py", line 349, in
_read_status
    raise BadStatusLine(line)
BadStatusLine
Request Headers:
  COOKIE: tg-visit=470841c0d3564a31e5de0a7361ba8f05b4af95f0
  Content-Length:
  ACCEPT-CHARSET: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  USER-AGENT: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:
1.8.1.13) Gecko/20080311 Firefox/2.0.0.13
  CONNECTION: keep-alive
  REFERER: http://bwaagooch:8080/
  X-REQUESTED-WITH: XMLHttpRequest
  HOST: bwaagooch:8080
  CONTENT-TYPE: application/x-www-form-urlencoded
  ACCEPT: text/xml,application/xml,application/xhtml+xml,text/
html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
  Remote-Addr: 10.3.18.87
  ACCEPT-LANGUAGE: en-us,en;q=0.5
  Content-Type: application/x-www-form-urlencoded
  ACCEPT-ENCODING: gzip,deflate
  KEEP-ALIVE: 300
 - agooch "GET /get_shards HTTP/1.1" 500 2875 "http://bwaagooch:8080/";
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/
20080311 Fi
refox/2.0.0.13"
2008-04-14 15:50:36,960 cherrypy.msg INFO ENGINE: SystemExit raised:
shutting down autoreloader

I see in httplib.py the following:

    def _read_status(self):
        # Initialize with Simple-Response defaults
        line = self.fp.readline()
        if self.debuglevel > 0:
            print "reply:", repr(line)
        if not line:
            # Presumably, the server closed the connection before
            # sending a valid response.
            raise BadStatusLine(line)

A couple of questions raise themselves: 1) why should this library act
differently when run in the context of a tg app with the exact same
inputs, and 2) if this is a problem with malformatted response in the
content of a tg app how should I work around it?

Many thanks in advance,
-ag

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to