BaseHTTPRequestHandler delays server response after "Popen"ing a program that waits for user input? (getline, fgets, etc.)

2009-04-16 Thread gburde...@gmail.com
I'm trying to write a very simple HTTP client/server program where the
client uploads a file via PUT using pycurl, and the server accepts the
file, "POpen"s a program, sends back "HELLO" to the client, then
displays "good morning".

The problem is when the "POpen"ed C++ program (test1.cpp below) waits
for some user input (via "getline()"; same thing happens with gets(),
fgets()). In this case, the "good morning" message stills get printed
on the server side, but the client doesn't get back "HELLO."

If the server program is terminated (by pressing ctrl-c), or a long
time (a few minutes?) passes, the client receives the "HELLO".

If the "POpen" program does not wait for user input (test2.cpp below),
the client immediately receives HELLO as expected.

Could this have anything to do with the getline() somehow blocking the
server response, even though it is POpened as a separate thread? How
can I solve this problem?

George.


**
server.py on server
**
#!/usr/bin/python
from subprocess import Popen, PIPE, STDOUT
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
# HTTP Server Methods
--
def do_PUT(self):
#print self.headers
length = int(self.headers.getheader('Content-Length'))
print 'Received PUT command, file length %d' % length
raw = self.rfile.read(length)
serverObj = Popen('./test1', stdin=PIPE, stdout=PIPE,
stderr=STDOUT)# Try replace test1 with test2
while True:
line = serverObj.stdout.readline()
print line,
if line.startswith('TYPE SOMETHING'):
break
self.wfile.write('HELLO!!!')
print 'good morning'
server = HTTPServer(('', 58621), Handler)
server.serve_forever()

**
test1.cpp on server:
**
#include 
using namespace std;
int main()
{
char string[256];
cout << "This is a test program" << endl;
cout << "TYPE SOMETHING" << endl;
cin.getline (string,256);
return 0;
}

**
test2.cpp on server:
**
#include 
using namespace std;
int main()
{
cout << "This is a test program" << endl;
cout << "TYPE SOMETHING" << endl;
return 0;
}

**
client.py on client:
**
#!/usr/bin/python
import os, sys
import pycurl
url = 'xxx'
class textBuffer:
def __init__(self):
self.contents = ''
def write(self, buf):
self.contents = self.contents+buf
# Initialize rawFileUploader, which uploads raw file and requests s2t
rawFileUploader = pycurl.Curl()
rawFileUploader.setopt(pycurl.URL, url)
rawFileUploader.setopt(pycurl.UPLOAD, 1)
while 1:
filename = raw_input('Enter filename (CTRL-C to quit)>')
rawFile = open(filename,'rb')
text = textBuffer()
rawFileUploader.setopt(pycurl.READDATA, rawFile)
rawFileUploader.setopt(pycurl.WRITEFUNCTION, text.write)
rawFileUploader.setopt(pycurl.INFILESIZE, os.path.getsize(filename))
rawFileUploader.perform()
rawFile.close()
print text.contents
rawFileUploader.close()

--
http://mail.python.org/mailman/listinfo/python-list


Default return values for out-of-bounds list item

2010-01-21 Thread gburde...@gmail.com
Is there a built-in method in python that lets you specify a "default"
value that will be returned whenever you try to access a list item
that is out of bounds? Basically, it would be a function like this:

def item(x,index,default):
   try:
  return x[index]
   except IndexError:
  return default

So if a=[0,1,2,3], then item(a,0,44)=0, item(a,1,44)=1, and item(a,
1000,44)=44, item(a,-1000,44)=44

What I want to know is whether there is a built-in method or notation
for this. What if, for example, we could do something like a
[1000,44] ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Something confusing about non-greedy reg exp match

2009-09-06 Thread gburde...@gmail.com
If I do this:

import re
a=re.search(r'hello.*?money',  'hello how are you hello funny money')

I would expect a.group(0) to be "hello funny money", since .*? is a
non-greedy match. But instead, I get the whole sentence, "hello how
are you hello funny money".

Is this expected behavior? How can I specify the correct regexp so
that I get "hello funny money" ?


-- 
http://mail.python.org/mailman/listinfo/python-list


Expanding a vector by replicating elements individually

2010-09-21 Thread gburde...@gmail.com
Given

m=numpy.array([[1, 2, 3]])

I want to obtain

array([[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]])

One way I've found to do this is:

numpy.reshape(numpy.tile(m,(4,1)),(12,1),'f').T

Another way is:

numpy.reshape(numpy.tile(m,(4,1)).flatten(1),(1,12))

Is there a simpler way to do this, without having to go jump through
so many hoops?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Expanding a vector by replicating elements individually

2010-09-24 Thread gburde...@gmail.com
On Sep 22, 1:30 am, Peter Otten <__pete...@web.de> wrote:
>
> array([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3])
>
> Peter

Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list