[Tutor] Capturing and parsing over telnet
I want to write a program that connects to a TCP port using telnet, and issues commands, parsing the output the command provides, and then issuing another command. This might look like this: $ telnet water.fieldphone.net 7456 Welcome to water, enter your username >_ sheep Enter your password >_ sheep123 >_ examine here [some info to parse] [.] [.] >_ some command based on parsing the previous screen [more info to parse] [.] [.] I am confident I can parse the info, if I can read it in. I am not sure how to handle the telnet I/O How should I proceed? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Capturing and parsing over telnet
Stephen Nelson-Smith wrote: > I want to write a program that connects to a TCP port using telnet, > and issues commands, parsing the output the command provides, and then > issuing another command. > > This might look like this: > > $ telnet water.fieldphone.net 7456 > Welcome to water, enter your username >> _ sheep > Enter your password >> _ sheep123 >> _ examine here > [some info to parse] > [.] > [.] >> _ some command based on parsing the previous screen > [more info to parse] > [.] > [.] > > I am confident I can parse the info, if I can read it in. > > I am not sure how to handle the telnet I/O > > How should I proceed? > > S. > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > How about pexpect; http://www.noah.org/wiki/Pexpect -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Capturing and parsing over telnet
Hi, > How about pexpect; > http://www.noah.org/wiki/Pexpect Ah yes - I've used that before to good effect. ATM I'm playing with telnetlib. Is there a way to read everything on the screen, even if I don't know what it will be? eg: c = telnetlib.Telnet("test.lan") c.read_until("name: ") c.write("test\n") c.read_until("word: ") c.write("test\n") And now I don't know what I will see - nor is there a prompt which indicates that the output is complete. I effectively want something like c.read_everything() I tried c.read_all() but I don't get anything from that. Suggestions? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Capturing and parsing over telnet
Hi, > I effectively want something like c.read_everything() Looks like read_very_eager() does what I want. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] attribute of built-in type
On Sat, Nov 29, 2008 at 5:09 PM, spir <[EMAIL PROTECTED]> wrote: > Kent Johnson a écrit : >> On Fri, Nov 28, 2008 at 6:01 PM, spir <[EMAIL PROTECTED]> wrote: >>> Kent Johnson a écrit : >> OK, functions (and methods, which are also functions, both of which >> are instances of some builtin type), classes (which are instances of >> type or classobj) and modules all have __name__ attributes. > > You're right, actually, as I was studying a bit how /some/ built-in types > work, and they all had a __name__, I had a wrong impression about that > attribute. > >>> Anyway, do you have an idea how to let custom objects inherit such >>> attributes (*)? If they were of a custom type, one would just do it with >>> inheritance (multiple, if needed). Now, this does not seem to work with >>> buil-tins -- or I was unable to find the proper way. >> >> I don't understand this paragraph. When you say "custom objects" do >> you mean classes or instances? Custom classes do have __name__ >> attributes; instances in general do not, other than the special cases >> mentioned above. >> >> I *think* what you want is to be able to say something like >> class Foo(object): pass >> myFoo = Foo() >> and then have >> foo.__name__ == 'myFoo' > > Exactly, "custom objects" was a shortcut for "objects that are instances of > custom types". > >> Is that right? If so, first recognize that the object can be assigned >> to many names: >> foo = bar = baz = Foo() >> so what you are asking is not well defined. > > Right. I just need the first one. The object's birthname ;-) ; whatever it > may be in case of "sequential naming", like in your example (which can > happen in my case). > >> Second, search on comp.lang.python for some ideas, this request comes >> up there from time to time. >> >> Third, how about passing the name to the constructor, or assigning the >> attribute yourself? >> myFoo = Foo('myFoo') >> or >> myFoo = Foo() >> myFoo.__name__ = 'myFoo' >> Yes, you have to type the name more than once... > > Actually, it is not an big issue. There are workarounds, anyway (such as > exploring the current scope's dict /after/ object creation, and assigning > its name back to an attribute -- this can also be automatised for numerous > objects). > Still, it would help & spare time & energy... This is also for me (and > hopefully other readers) an opportunity to explore corners of the python > universe ;-) > >> AFAIK Python does not have any hook to modify assignment which I think >> is what you want. You might be able to do something like this with the >> ast module in Python 2.6. You would have to load the code for a >> module, compile it to the AST, modify the AST and compile it to code. >> Some examples of this: >> http://code.activestate.com/recipes/533145/ >> http://pyside.blogspot.com/2008/03/ast-compilation-from-python.html >> >> http://lucumr.pocoo.org/cogitations/2008/03/30/high-level-ast-module-for-python/ >> >> I know you explained before but I still don't understand *why* you >> want to do this... > > First, this problem occurs while I'm trying to use code of an external > module for a project of mine. My case is probably specific, otherwise this > issue would have been adressed long ago. > These objects are kinds of "models" -- they control the generation of other > objects -- but they are not classes, and can't be. As a consequence, the > generated objects do not know what they actually are. They have a type, of > course, but this type is general and not related to the real nature of the > objects that share it. It would be a first step if the generators first > would themselves know 'who' they are; then they would be able to pass this > information to their respective "chidren". This would be a very small and > harmful hack in the module's code (which is nice to study, not only for its > organisation, also because it is not built upon further modules). Do you know that you can probably just assign a __name__ attribute to the objects? Or name, or whatever you like? In [13]: class Foo(object): pass : In [14]: f=Foo() In [15]: f.name --- AttributeErrorTraceback (most recent call last) /Users/kent/ in () AttributeError: 'Foo' object has no attribute 'name' In [16]: f.name = 'f' In [17]: f.name Out[17]: 'f' This will work for custom objects that do not have a __slots__ attribute. Perhaps you could wrap the creation of the objects in a function that gives them a name? >>> (*) The point is that they can't be written by hand. How would you do it >>> e.g. that an attribute automatically holds the objects own name? You can >>> play with the namespace's dict, but it's the same thing. (It's like >>> inheriting for instance an integer's behaviour: yes, you can rewrite it >>> from >>> scratch.) >>> class myInt(int): works >>> class myFunc(function): works not >>> TypeError: Error when calling the metaclass bases >>>type
Re: [Tutor] Text Scatter Plots?
On Mon, Sep 29, 2008 at 6:34 AM, Wayne Watson <[EMAIL PROTECTED]> wrote: > Is there a text graphics module that does say scatter plots or histograms? > I'm thinking of stuff prior to the graphics era of computing. I'm looking > for something really simple. Not simple but maybe helpful: http://www-rcf.usc.edu/~kaizhu/work/asciiporn/ Despite the name it has nothing to do with porn, AFAICT. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Reading gzip files
I'm reading gzip files and writing the content out to a text file line by line. The code is simply: import gzip list_zipfiles = dircache.listdir(zipfolder) writefile = "out_file.txt" fw = open(writefile, 'w') for ziparchive in list_zipfiles: zfile = gzip.GzipFile(zipfolder + ziparchive, "r") for line in zfile: fw.write(line) zfile.close() fw.close() The Traceback is: Traceback (most recent call last): File "py", line 47, in for line in zfile: File "C:\Python25\lib\gzip.py", line 444, in next line = self.readline() File "C:\Python25\lib\gzip.py", line 399, in readline c = self.read(readsize) File "C:\Python25\lib\gzip.py", line 227, in read self._read(readsize) File "C:\Python25\lib\gzip.py", line 275, in _read self._read_eof() File "C:\Python25\lib\gzip.py", line 311, in _read_eof raise IOError, "CRC check failed" IOError: CRC check failed I've checked the Python docs and online but cannot find a solution to the problem. Thanks. Dinesh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading gzip files
"Dinesh B Vadhia" <[EMAIL PROTECTED]> wrote I'm reading gzip files and writing the content out to a text file line by line. File "C:\Python25\lib\gzip.py", line 275, in _read self._read_eof() File "C:\Python25\lib\gzip.py", line 311, in _read_eof raise IOError, "CRC check failed" IOError: CRC check failed I've checked the Python docs and online but cannot find a solution to the problem. Thanks. At great risk of stating the obvious but have you actually checked that the CRC of the gzipped file is correct? Does the problem only happen with this file or with all gzipped files? And are the files actually zipped by gzip or by some other "compatible" program? If other does it work for genuine gzipped files? Just some ideas to play with. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading gzip files
Dinesh B Vadhia wrote: > I'm reading gzip files and writing the content out to a text file line > by line. The code is simply: > > import gzip > list_zipfiles = dircache.listdir(zipfolder) > writefile = "out_file.txt" > fw = open(writefile, 'w') > > for ziparchive in list_zipfiles: > zfile = gzip.GzipFile(zipfolder + ziparchive, "r") > for line in zfile: > fw.write(line) > zfile.close() > fw.close() I am learning also. I came up with this; #!/usr/bin/python import tarfile tFile = tarfile.open("/home/david/zip_files/zip.tar.gz", "r") for f in tFile.getnames(): print f tFile.close() #fname = "out.txt" #fobj = open(fname, 'w') #for line in f: #fobj.write(line + '/n') #tFile.close() #fobj.close() My problem is I can not figure out how to write to the file with new lines, as you can see my attempts at the bottom. This works fine in Linux; ./py_list_zip.py >> out.txt but I want to learn how to do it within python, plus to understand. any hints :) thanks -david -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading gzip files
David wrote: > #!/usr/bin/python > import tarfile > tFile = tarfile.open("/home/david/zip_files/zip.tar.gz", "r") > for f in tFile.getnames(): > print f > tFile.close() > #fname = "out.txt" > #fobj = open(fname, 'w') > #for line in f: > #fobj.write(line + '/n') > #tFile.close() > #fobj.close() > > My problem is I can not figure out how to write to the file with new > lines, as you can see my attempts at the bottom. This works fine in Linux; > ./py_list_zip.py >> out.txt > > but I want to learn how to do it within python, plus to understand. > > any hints :) > > thanks > -david > > > oops should have been; fobj.write(line + '\n') still does not work, now here is the output from the print statment; zip/ zip/py_database.py zip/print_time.py and out.txt z i p / p r i n t _ t i m e . p y -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading gzip files
Hi Alan A bunch of gzipped files are read with the majority working but a few don't. I don't know if these files were originally zipped with gzip but I'd guess that they were. Strangely, for the files that don't work I can read/print the file almost to the end and then it falls over with the CRC error. Dinesh Message: 5 Date: Mon, 1 Dec 2008 00:57:04 - From: "Alan Gauld" <[EMAIL PROTECTED]> Subject: Re: [Tutor] Reading gzip files To: tutor@python.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original "Dinesh B Vadhia" <[EMAIL PROTECTED]> wrote > I'm reading gzip files and writing the content out to a text file > line by line. > File "C:\Python25\lib\gzip.py", line 275, in _read >self._read_eof() > File "C:\Python25\lib\gzip.py", line 311, in _read_eof >raise IOError, "CRC check failed" > IOError: CRC check failed > > I've checked the Python docs and online but cannot find a > solution to the problem. Thanks. At great risk of stating the obvious but have you actually checked that the CRC of the gzipped file is correct? Does the problem only happen with this file or with all gzipped files? And are the files actually zipped by gzip or by some other "compatible" program? If other does it work for genuine gzipped files? Just some ideas to play with. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor