strange sockets
Hi, I'm preparing a python server that sends java classes and resources to custom java class loader. In order to make it faster I don't want to use URLClassLoader that uses HTTP protocol 1.0 and for each class/resource creates own connection. Instead I'd like to use raw sockets with simple protocol: - class loader sends a line terminated with \n with resource to get - python server reads that line, gets the file and sends back an integer with file length and then the file itself - class loader reads a lenght integer and then reads the remainig data The problem is when I try to read several files the first one is read quite fast, but the rest is read 40 x slower. For example (time is in seconds): % python client.py client.py client.py client.py server.py server.py init 0.00066089630127 client.py 0.000954866409302 client.py 0.0408389568329 client.py 0.0409188270569 server.py 0.0409059524536 server.py 0.0409259796143 what's wrong here? thanks, skink client.py import socket, sys, struct, time HOST = 'localhost' PORT = 8080 t1 = time.time() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) t2 = time.time() print "init", t2-t1 for arg in sys.argv[1:]: t1 = time.time() s.send(arg + "\n") len, = struct.unpack("!i", s.recv(4)) data = s.recv(len) t2 = time.time() print arg, t2-t1 s.close() server.py import socket, struct, binascii HOST = '' PORT = 8080 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) while 1: s.listen(1) conn, addr = s.accept() print 'Connected by', addr f = conn.makefile() while 1: resource = f.readline().rstrip() print "[%s]" % resource if not resource: break data = open(resource, "rb").read() conn.sendall(struct.pack("!i", len(data))) conn.sendall(data) conn.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: strange sockets
Sion, > Solutions: either change > > >> conn.sendall(struct.pack("!i", len(data))) >> conn.sendall(data) > > > to > > conn.sendall(struct.pack("!i", len(data)) + data) > > or after creating conn > > conn.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) > > to disable Nagle. thank yuo so much, both solutions work perfect! %python client.py client.py client.py client.py init 0.00101184844971 client.py 0.000586986541748 client.py 0.000448942184448 client.py 0.000470161437988 I think that I'll use the second one skink -- http://mail.python.org/mailman/listinfo/python-list
Re: strange sockets
Bryan, > > Sion Arrowsmith is right about what causes the delay. > Just in case your real code looks like this, I'll note: > >> len, = struct.unpack("!i", s.recv(4)) >> data = s.recv(len) yes, my mistake ;) > > > First, you almost certainly don't want to use the name 'len'. > Ought not to be allowed. Second, recv can return fewer bytes > than requested, even when the connection is still open for > reading. You might replace the lines above with (untested): > > length = struct.unpack("!i", s.recv(4)) > data = [] > while length: > data.append(s.recv(length)) > length -= len(data[-1]) > data = ''.join(data) > > i know, i know, i sent fake python client: the real will be done in java. > There's still a robustness problem, but in the absense of errors > and malice, that should work. I think. > > -- http://mail.python.org/mailman/listinfo/python-list
Re: strange sockets
Sion Arrowsmith wrote: > > conn.sendall(struct.pack("!i", len(data)) + data) > > or after creating conn > > conn.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) > > to disable Nagle. > Sion, thank you for your help, it works but... it works when client & server is in python i tried both solutions and they work when client is client.py they both don't work when client is java client when i tried to connect python's server by java client i have the same: % java Loader server.py server.py server.py init 29 server.py reading 631 1 server.py reading 631 40 server.py reading 631 41 why? thanks, skink. -- http://mail.python.org/mailman/listinfo/python-list
Re: strange sockets
Steve Holden wrote: > Skink wrote: > >> Sion Arrowsmith wrote: >> >>> conn.sendall(struct.pack("!i", len(data)) + data) >>> >>> or after creating conn >>> >>> conn.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) >>> >>> to disable Nagle. >>> >> >> >> Sion, >> >> thank you for your help, >> >> it works but... >> it works when client & server is in python >> i tried both solutions and they work when client is client.py >> they both don't work when client is java client >> when i tried to connect python's server by java client i have the same: >> >> % java Loader server.py server.py server.py >> init 29 >> server.py reading 631 1 >> server.py reading 631 40 >> server.py reading 631 41 >> >> why? >> > Seems to me that should probably be a question for comp.lang.java. ok, my falt. again... ;) i forgot to use Buffered[Input|Output]Stream > > regards > Steve -- http://mail.python.org/mailman/listinfo/python-list
traceback from embedded python
hi, I'm using boost::python for calling some python code and when the exception is thrown I want to get type, value and traceback of it. The problem is with traceback: I got only *one* frame (the first one) // this always returns Py_None tb_frame = PyObject_GetAttrString(tb_frame, "f_back"); What I'm doing wrong? void Deployer::showError() { //PyErr_Print(); PyObject *exc_type, *exc_value, *exc_traceback, *pystring; PyErr_Fetch(&exc_type, &exc_value, &exc_traceback); // type works pystring = PyObject_Str(exc_type); char *type = QString(python::extract(pystring)); Py_XDECREF(pystring); // value works pystring = PyObject_Str(exc_value); char *value = QString(python::extract(pystring)); Py_XDECREF(pystring); // traceback does't work ;( PyObject *tb_frame = PyObject_GetAttrString(exc_traceback, "tb_frame"); while (tb_frame != Py_None) { PyObject *f_lineno = PyObject_GetAttrString(tb_frame, "f_lineno"); int lineno = python::extract(f_lineno); Py_XDECREF(f_lineno); PyObject *f_code = PyObject_GetAttrString(tb_frame, "f_code"); PyObject *co_filename = PyObject_GetAttrString(f_code, "co_filename"); char *filename = python::extract(co_filename); Py_XDECREF(f_code); Py_XDECREF(co_filename); PyObject *tmp = tb_frame; //PyObject_Print(tb_frame, stderr, Py_PRINT_RAW); //fprintf(stderr, "\n"); tb_frame = PyObject_GetAttrString(tb_frame, "f_back"); //PyObject_Print(tb_frame, stderr, Py_PRINT_RAW); //fprintf(stderr, "\n"); Py_XDECREF(tmp); } Py_XDECREF(exc_type); Py_XDECREF(exc_value); Py_XDECREF(exc_traceback); } thanks, Skink -- http://mail.python.org/mailman/listinfo/python-list
django's view.py as class not just methods
Hi, I'm relatively new to django and maybe my question is stupid, but... Is it possible to map in urls.py some url not to function in views.py (which has first argument with HttpRequest) but to some class method? In that case each instance of such class would be created when session starts and for subsequent calls would be served as self ? I know, I know that HttpRequest has session member and I can use it. But maybe it would be good idea to have such url ==> class.method mapping. thanks, skink -- http://mail.python.org/mailman/listinfo/python-list
Re: django's view.py as class not just methods
Rob Wolfe wrote: > > I didn't try it with django but maybe closure is the solution. > Try this: > > > index, detail and download functions can be mapped > in urls.py now. > Rob, thanks a lot. is gonna be good starting point skink -- http://mail.python.org/mailman/listinfo/python-list
models & editors in PyQt4
Hi, I created simple property classes with editing option, but since i'm not too much experienced in PyQt4 i'd like to ask if i handle ColorProperty changing right. Any other Property has its own editor and their control flow is imho ok. Hovewer i'm not sure about ColorProperty. What i do is: in createEditor(self, parent, option, index) i call QtGui.QColorDialog.getColor() and return None What do you think? Is it Ok? thanks, skink # import sys from PyQt4 import QtGui, QtCore class PropertyDelegate(QtGui.QItemDelegate): def __init__(self, model): QtGui.QItemDelegate.__init__(self, None) self.model = model def createEditor(self, parent, option, index): if index.column() == 0: return None item = self.model.getItem(index) return item.createEditor(parent, option, index) def setEditorData(self, editor, index): item = self.model.getItem(index) item.setEditorData(editor, index) def setModelData(self, editor, model, index): item = self.model.getItem(index) item.setModelData(editor, model, index) #def updateEditorGeometry(self, editor, option, index): #editor.setGeometry(option.rect) def paint(self, painter, option, index): item = self.model.getItem(index) if isinstance(item, ColorProperty) and index.column() == 1: item.paint(painter, option, index) return QtGui.QItemDelegate.paint(self, painter, option, index) class SimpleModelItem: def __init__(self, parent=0): self._index = [] self._children = [] self._parent = parent if parent: parent._children.append(self) self._parent = parent def parent(self): return self._parent def children(self): return self._children def addIndex(self, index): self._index.append(index) def childId(self, child): return id(self._children[child]) def index(self, column=0): return self._index[column] class Property(SimpleModelItem): def __init__(self, parent=0, name="", data=[]): SimpleModelItem.__init__(self, parent) self._data = data self._name = name def data(self, index): return self._data[index.column()] def name(self): return self._name def createEditor(self, parent, option, index): return None def setEditorData(self, editor, index): return def setModelData(self, editor, model, index): return class StringProperty(Property): def __init__(self, parent, name, t): Property.__init__(self, parent, name, [t, ""]) def createEditor(self, parent, option, index): editor = QtGui.QLineEdit(parent) return editor def setEditorData(self, editor, index): value = index.model().getObjectData(self.name()) editor.setText(value) def setModelData(self, editor, model, index): index.model().setObjectData(self.name(), editor.text()) def data(self, index): if index.column() == 0: return self._data[0] else: return str(index.model().getObjectData(self.name())) class IntegerProperty(Property): def __init__(self, parent, name, t): Property.__init__(self, parent, name, [t, ""]) def createEditor(self, parent, option, index): editor = QtGui.QSpinBox(parent) editor.setMaximum(256*256) return editor def setEditorData(self, editor, index): value = index.model().getObjectData(self.name()) editor.setValue(value) def setModelData(self, editor, model, index): index.model().setObjectData(self.name(), editor.value()) def data(self, index): if index.column() == 0: return self._data[0] else: return str(index.model().getObjectData(self.name())) class SizeProperty(Property): def __init__(self, parent, name, t): Property.__init__(self, parent, name, [t, ""]) self.items = [] self.items.append(IntegerProperty(self, name+":x", "x")) self.items.append(IntegerProperty(self, name+":y", "y")) self.items.append(IntegerProperty(self, name+":w", "width")) self.items.append(IntegerProperty(self, name+":h", "height")) class ColorProperty(Property): def __init__(self, parent, name, t): Property.__init__(self, parent, name, [t, ""]) def createEditor(self, parent, option, index): color = QtGui.QColorDialog.getColor() if color.isValid(): index.model().setObjectData(self.name(), color) return None def paint(self, painter, option, index): r = option.rect
Re: models & editors in PyQt4
David Boddie wrote: > > It should be OK - it shouldn't crash, anyway. It depends on the view > doing the right thing when it finds that it hasn't received an editor > widget. > > You could create an empty placeholder widget in the createEditor() > method, call QColorDialog.getColor() with the existing color from the > model in setEditorData(), record the color returned by the dialog in > some internal instance variable, and finally set the data in the model > when setModelData() is called: > > class ColorProperty(Property): > ... > def createEditor(self, parent, option, index): > return QtGui.QWidget(parent) > def setEditorData(self, editor, index): > self.color = QtGui.QColorDialog.getColor( > index.model().getObjectData(self.name())) > def setModelData(self, editor, model, index): > if self.color.isValid(): > index.model().setObjectData(self.name(), self.color) thanks for tip, i'll check it how it works. > ... > > I find it strange that you have to triple-click to edit any of the > items in your example. Do you see the same behaviour? oh, this is default Qt behavoiur: first click selects row, second select editor (for ColorProperty, IntProperty & StringProperty you can now change the value) but third click is required only for properties w/ QCombobox editor (EnumProperty & BooleanProperty) ... skink > > David > -- http://mail.python.org/mailman/listinfo/python-list
SOAPpy
hi, there is a soap service: http://waluty.k2.pl/ws/NBPRates.asmx the question is why when i call GetRateValue method i always get '0' result? >>> import SOAPpy >>> srv = SOAPpy.WSDL.Proxy("http://waluty.k2.pl/ws/NBPRates.asmx?WSDL";) >>> srv.GetAllCodes() 'AUD CAD CHF CYP CZK DKK EEK EUR GBP HKD HUF JPY LTL LVL MTL NOK RUB SEK SIT SKK UAH USD XDR ZAR ' >>> srv.GetRateValue('USD') '0' it seems that methods with no params are working but these that take at least one param not. you can check it by going to http://www.soapclient.com/soaptest.html and entering http://waluty.k2.pl/ws/NBPRates.asmx?WSDL as WSDL File Address the rate should be ~3.1379 or something thanks, pb -- http://mail.python.org/mailman/listinfo/python-list
Re: PyQt 4.0beta1 Released
Phil Thompson wrote: > Riverbank Computing is pleased to announce the release of PyQt v4.0beta1 > available from http://www.riverbankcomputing.co.uk/pyqt/. > > PyQt is a comprehensive set of Qt bindings for the Python programming language > and supports the same platforms as Qt (Windows, Linux and MacOS/X). Like Qt, > PyQt is available under the GPL and a commercial license. > > PyQt v4 supports Qt v4 (http://www.trolltech.com/products/qt/index.html). > PyQt v3 is still available to support earlier versions of Qt. > Phil, great news! However I have one question: my qt4 is built with QT_NO_ACCESSIBILITY and when I try to build pyqt I got errors no such method QWidget.setAccessibleName What should I do? Rebuild qt4 without QT_NO_ACCESSIBILITY #define? My qt is 4.1.2 skink -- http://mail.python.org/mailman/listinfo/python-list
pyqt v3.* and v4.*
Hi, Is it possible to have both versions of pyqt (in my case 3.14.1 and 4.0)? Version 3 is built using sip 4.2.1, version 4 is built using sip 4.4.3 When I run pyqt 4.0 (but with installed sip 4.2.1) I get: from PyQt4 import QtCore, QtGui TypeError: invalid argument to sipBadCatcherResult() When I run pyqt 3.14 (but with installed sip 4.4.3) I get: import qt Segmentation fault Both 3.14 and 4.0 work, but not when installed together. skink -- http://mail.python.org/mailman/listinfo/python-list
Re: pyqt v3.* and v4.*
Phil Thompson wrote: > > > Install the latest versions of PyQt3 and SIP. Thanks Phil! v4.0 & v3.16 work with sip 4.4.3 ;) skink -- http://mail.python.org/mailman/listinfo/python-list