[issue26657] Directory traversal with http.server and SimpleHTTPServer on windows
Philipp Hagemeister added the comment: Update testcase, and call split before splitdrive -- Added file: http://bugs.python.org/file42319/fix-path-traversal-26657.patch ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue26657> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue26657] Directory traversal with http.server and SimpleHTTPServer on windows
Philipp Hagemeister added the comment: Please find attached a patch which adds a testcase for Windows (on all platforms) as well as code to fix the problem. Since os.path.split returns everything after the final slash/backslash, it only needs to be called once. Note that the usage of posixpath is correct and only relates to the URL parsing - it powers foo/bar/../../ . The path elements may indeed contain backslashes - that's why we call os.path.split on them. -- keywords: +patch nosy: +phihag Added file: http://bugs.python.org/file42318/fix-path-traversal-26657.patch ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue26657> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: Download all youtube favorites with youtube-dl script
-BEGIN PGP SIGNED MESSAGE- Hash: SHA512 Hi Bill, the best way to ask for features or file bugs on youtube-dl is asking us at http://github.com/rg3/youtube-dl/issues . Luckily, I this list from time to time ;) Simply use youtube-dl --username u...@gmail.com --password secret :ytfav Best, Philipp On 09/26/2013 05:13 PM, Bill wrote: I have been using the script youtube-dl http://rg3.github.io/youtube-dl/ And I was wondering if there is a way to download all of a user's favorites or uploads. The script has a functionality to download all videos in a txt file. So if there is a way using the youtube API or JSON (none of which I am familiar with) to bring up a list of all these videos then it'd be a simple case putting these urls into a file. The problem is youtube displays favorites or user uploads in pages or infinite scroll. So it is difficult to access them by the BeautifulSoup module. What do you suggest? -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.14 (GNU/Linux) iQIcBAEBCgAGBQJSRFQmAAoJENtLVMukgmoYaz0P/ifoPqcx4dpShXyigwGxzhsa ht8sjIP8n3bNJ+J7Jvx0uM6Sv/Hu/U1CC09G1pT9nquHDYYvmko+MoBkXbc8JmlC R0qPhX0I2xGH9G1EL+83J4zzte3K7p5ErLY1V1S9cWuBKot16eKtrxhIgWxTMfE1 l93az4Uz4YLzUPYEXSO7pNX9cvxyrEjsqSJE3Jftn5ZqbUO46M+7gMKG2g46C/W4 IQtg2v9cHE3xKV5c+wCFjjIHtOGg+leOTMFdiD6oIa/uNfV/3tzpiyQ1e2QgyrvU UK4zz3TxqxWswDTrxRdc7fFNAGoNSrxU2hwyvjc/CGehORv7ktBjJJbtt3PfvHsi nov+OEIToDFTO1nCzQ39qQP0Ibl0LpNbvJGWyJqubFsZd0uiU1EwONXFchNwHeil Yce4DT8Tzf4zv6y3YNJCz1RCM1G308vQav539w6D7vfIUc5F28gnWKkd5NIcCpyu URVWp0HxV7d+kCekbxhnd+Ah/XvsrHhkI3cxOHvc1QjGwToRWawJQT0LD72E6PqW MmnOUTZhrxebyAx1HEt45l19fuW/TfwCWWzAtRscr8uFLFf0/Hwm41tSo8FjxqK2 OVZAckmVYIKP0F+u6hcSg/INl6rs6R+Co4/S8aFdKh2N16wmwQ7hevoTIpXIFwwK hkpwAUdlQxIWzSe8uJMV =nRz7 -END PGP SIGNATURE- -- https://mail.python.org/mailman/listinfo/python-list
Re: Issues a longer xpath expression
Hi anonymous, your code is working perfectly right. It's just that the only time that you find anything matching //div[@class=col f-cb] is this one: div class=col f-cb div class=name s-fc3 f-fl名称/div div class=down s-fc3 f-fl视频下载/div div class=desc s-fc3 f-fl课程简介/div /div And obviously, there's no a in there, so the xpath won't match. Cheers, Philipp On 02/22/2013 02:24 AM, python wrote: I am having issues with the urllib and lxml.html modules. Here is my original code: import urllib import lxml.html down='http://v.163.com/special/visualizingdata/' file=urllib.urlopen(down).read() root=lxml.html.document_fromstring(file) xpath_str=//div[@class='down s-fc3 f-fl']/a urllist=root.xpath(xpath_str)for url in urllist:print url.get(href) When run, it returns this output: http://mov.bn.netease.com/movieMP4/2012/12/A/7/S8H1TH9A7.mp4 http://mov.bn.netease.com/movieMP4/2012/12/D/9/S8H1ULCD9.mp4 http://mov.bn.netease.com/movieMP4/2012/12/4/P/S8H1UUH4P.mp4 http://mov.bn.netease.com/movieMP4/2012/12/B/V/S8H1V8RBV.mp4 http://mov.bn.netease.com/movieMP4/2012/12/6/E/S8H1VIF6E.mp4 http://mov.bn.netease.com/movieMP4/2012/12/B/G/S8H1VQ2BG.mp4 But, when I change the line xpath_str='//div[@class=down s-fc3 f-fl]//a' into xpath_str='//div[@class=col f-cb]//div[@class=down s-fc3 f-fl]//a' that is to say, urllist=root.xpath('//div[@class=col f-cb]//div[@class=down s-fc3 f-fl]//a') I do not receive any output. What is the flaw in this code? it is so strange that the shorter one can work,the longer one can not,they have the same xpath structure! signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: pypi changelog api
Hi Gregg, to get a smaller response, you can simply pass in a timestamp, like this: client = xmlrpclib.ServerProxy('http://pypi.python.org/pypi') import time client.changelog(int(time.time() - 600)) [['vs.bootstrap.plonetheme', '1.0.1', 1361451748, 'update description, classifiers'], ['numericalunits', '1.11', 1361451759, 'new release'], ['numericalunits', '1.11', 1361451760, 'add source file numericalunits-1.11.tar.gz'], ['autobahn_rce', '0.6.0', 1361451798, 'remove'], ['vs.bootstrap.plonetheme', '1.0.1', 1361451816, 'update description, classifiers'], ['vs.bootstrap.plonetheme', '1.0.1', 1361451882, 'update description, classifiers'], ['autobahn_rce', '0.5.9', 1361451956, 'new release'], ['autobahn_rce', '0.5.9', 1361451971, 'add source file autobahn_rce-0.5.9.tar.gz']] I don't think there's way to get older chunks of the changelog though. What would you need those for? If you need the entire changelog, just download it once (it's not that large, barely 40MB). Here it is, up until 1361452402 (now): http://phihag.de/2013/pypi-changelog-2013-02-20.json.bz2 What I'd like is a real-time push service of changelog entries, but I'm not certain that would be scalable. Cheers, Philipp On 02/18/2013 02:16 AM, Gregg Caines wrote: Hey all, I'm trying to write a script that tracks changes on pypi, and I've come across the xmlrpc interface, specifically the 'changelog' api. It's definitely what I'm looking for, but I get an absolutely massive xml response from it at once and I was hoping there might be either some way to page through it with mulitple requests, or a different-but-similar API. Thanks in advance, Gregg signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: pypi changelog api
Just FYI, PyPi already has an RSS feed: https://pypi.python.org/pypi?:action=rss And instead of mailing the author, you should probably open an issue (or better yet, a pull request) at https://github.com/crateio/crate.web/issues For that matter - how does crate.io update their PyPi mirror? Cheers, Philipp On 02/21/2013 02:52 PM, Michael Herman wrote: I'd love to see https://crate.io/ set up an API or at the very least an RSS feed for tracking changes. I've emailed the author about this. I think if enough people do, an RSS feed would be easy to setup. On Thu, Feb 21, 2013 at 5:33 AM, Philipp Hagemeister phi...@phihag.dewrote: Hi Gregg, to get a smaller response, you can simply pass in a timestamp, like this: client = xmlrpclib.ServerProxy('http://pypi.python.org/pypi') import time client.changelog(int(time.time() - 600)) [['vs.bootstrap.plonetheme', '1.0.1', 1361451748, 'update description, classifiers'], ['numericalunits', '1.11', 1361451759, 'new release'], ['numericalunits', '1.11', 1361451760, 'add source file numericalunits-1.11.tar.gz'], ['autobahn_rce', '0.6.0', 1361451798, 'remove'], ['vs.bootstrap.plonetheme', '1.0.1', 1361451816, 'update description, classifiers'], ['vs.bootstrap.plonetheme', '1.0.1', 1361451882, 'update description, classifiers'], ['autobahn_rce', '0.5.9', 1361451956, 'new release'], ['autobahn_rce', '0.5.9', 1361451971, 'add source file autobahn_rce-0.5.9.tar.gz']] I don't think there's way to get older chunks of the changelog though. What would you need those for? If you need the entire changelog, just download it once (it's not that large, barely 40MB). Here it is, up until 1361452402 (now): http://phihag.de/2013/pypi-changelog-2013-02-20.json.bz2 What I'd like is a real-time push service of changelog entries, but I'm not certain that would be scalable. Cheers, Philipp On 02/18/2013 02:16 AM, Gregg Caines wrote: Hey all, I'm trying to write a script that tracks changes on pypi, and I've come across the xmlrpc interface, specifically the 'changelog' api. It's definitely what I'm looking for, but I get an absolutely massive xml response from it at once and I was hoping there might be either some way to page through it with mulitple requests, or a different-but-similar API. Thanks in advance, Gregg -- http://mail.python.org/mailman/listinfo/python-list signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: pypi changelog api
On 02/21/2013 02:58 PM, Michael Herman wrote: Oh - and I haven't tried this site, but you may be able to set something up on there to email when the changelog is updated. http://www.changedetection.com/ They just query the whole page - I could do that myself, easily. But the problem is that that would mean lots of load on PyPi (if you query every minute or so) and outdated packages (if you query less often than that). Keeping a connection that PyPi would push to seems to be much cleaner. - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: ** Please Rank These Learning Sources **
http://learnpythonthehardway.org/book/ I have never used that myself, but I have seeen plenty of stackoverflow and student questions about it. In short, it's horrible. The book mostly consists of basic Python programs, and beginners often fail to grasp even the most basic structures demonstrated, mainly because they're not explained. The book has some of the easy-to-approach informality of, say, the Head First ones, but fails to contain the the actual explanations for it. (And I have no idea why one would start with Python 2.x, given that everything's strange in there - instead of relying on the intuitive grasp that both a and ä are single character strings, and that print is a function like any other, they have to work with a lot of magic and hand-waving) - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: any chance for contracts and invariants in Python?
I don't know anything about the status of this PEP or why it hasn't been implemented, but here's what strikes me as obviously complex: Doesn't one need to traverse the entire class hierarchy on every function call? So if I have class A: def foo(self): return 1 class B(A): inv: True def foo(self): post: __return__ == 2 return 2 Now, I have f = B().foo f() . What does Python do? If your answer is 1. Look up class of f 2. Check its invariant (succeeds) 3. Execute the function 4. Check post conditions of f (succeeds) 5. return 2 Then what will I get if I run any of the following programs: A.foo.__doc__ = 'inv: __return__ == 1' f() def _foo(self): 'post: __return__ == 3' A.foo = _foo f() A.__doc__ = 'inv: False' f() So any implementation has to choose one of the following: 1. Ignore invariants and postconditions of inherited classes - defeats the purpose. 2. Only respect definitions in classes and methods in the original definition, which would be unpythonic 3. Only respect the original definitions, for some value of original. Simarily, this would break monkey patching. 4. Update all subclasses whenever something changes. 5. Traverse the entire class hierarchy for every method call. Which option should be picked? Additionally, the reference implementation is not actually a fork of cpython (or a metaclass), but a Python script that - as far as I understand - I have to call manually to start using contracts. - Philipp On 14.02.2013 12:42, mrk...@gmail.com wrote: This PEP seems to be gathering dust: http://www.python.org/dev/peps/pep-0316/ I was thinking the other day, would contracts and invariants not be better than unit tests? That is, they could do what unit tests do and more, bc they run at execution time and not just at development time? signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: what’s the difference between socket.send() and socket.sendall() ?
socket.socket.send is a low-level method and basically just the C/syscall method send(3) / send(2). It can send less bytes than you requested. socket.socket.sendall is a high-level Python-only method that sends the entire buffer you pass or throws an exception. It does that by calling send until everything has been sent or an error occurs. If you're using TCP with blocking sockets and don't want to be bothered by internals (this is the case for most simple network applications), use sendall. Otherwise, use send and make sure to read and process its return value. - Philipp On 01/07/2013 11:35 AM, iMath wrote: what’s the difference between socket.send() and socket.sendall() ? It is so hard for me to tell the difference between them from the python doc so what is the difference between them ? and each one is suitable for which case ? signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
[issue16465] dict creation performance regression
New submission from Philipp Hagemeister: On my system, {} has become significantly slower in 3.3: $ python3.2 -m timeit -n 100 '{}' 100 loops, best of 3: 0.0314 usec per loop $ python3.3 -m timeit -n 100 '{}' 100 loops, best of 3: 0.0892 usec per loop $ hg id -i ee7b713fec71+ $ ./python -m timeit -n 100 '{}' 100 loops, best of 3: 0.0976 usec per loop Is this because of the dict randomization? -- components: Interpreter Core messages: 175503 nosy: phihag priority: normal severity: normal status: open title: dict creation performance regression type: performance versions: Python 3.3, Python 3.4 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16465 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: Blue Screen Python
On 10/09/2012 09:37 AM, mikcec82 wrote: In my script I open and close an html (in a FOR cycle); could be this the problem? Unless you're running your Python script as a kernel driver (and you can't do that accidentally), there is no way that your user-space program should cause a bluescreen. This is an error in Windows (or one of the drivers), not in Python or your program. What you can do is insert your Windows DVD, boot from it, and click Repair. Or is it possible that Python 2.7 is not compatible with Win7? No, even if a user-space program could legitimately cause a bluescreen, Python 2.7 still works fine one thousands of Win7 machines. Cheers, Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: consistent input() for Python 2 and 3
On 08/02/2012 11:49 AM, Ulrich Eckhardt wrote: try: # redirect input() to raw_input() like Python 3 input = raw_input except NameError: # no raw input, probably running Python 3 already pass What do you think? Any better alternatives? That's the generic solution, see http://python3porting.com/differences.html#input-and-raw-input . In my experience, it seems that input's main function is to allow beginners to learn the language, or to be used in short scripts. For a serious application, either curses or moving the input to the invocation arguments is often a better choice. - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Error help
Let's go thorugh this program: On 08/02/2012 12:58 PM, danielashi...@googlemail.com wrote: import cPickle, pickle you probably want to transparently fall back to pickle if cPickle is not available. print 'WELLCOME TO THE LIBET CLOCK EXPERIMENT DATA ANALYSIST' file=raw_input('\nPLEASE ENTER THE NAME OF THE FILE YOU WISH TO ANALYSE: ') This should be a command-line option. Look for optparse, or use sys.argv[1] pickle_file=open(file, 'r') data=cPickle.load(pickle_file) You're opening a file in text mode, and leaking the handle. file_name=file-'pck' + 'txt' You cannot subtract strings. You probably want to use + . Also, move this assignment down to where it's needed partifipant_info=data'Participant Info'] You later use participant_info. typo? first_block_data=data['First block data'] second_block_data=data['Second block data'] first_block_estimates=first_block_data['Estimated times'] first_block_times=first_block_data['Actual times'] second_block_estimates=second_block_data['Estimated times'] second_block_times=second_block_data['Actual times'] This is not a useful data structure. You want: blocks = [{ 'estimates': data[name]['Estimated times'], 'actuals': data[name]['Actual Times'], } for name in [First block data, Second block data}] or better yet, a sane pickle format. def firstBlockDifferences(): This method should be called blockDifferences, and take a block as an argument, and return differenes. See http://docs.python.org/tutorial/controlflow.html#defining-functions for an introduction. print '\nCALCULATING AVERAGE DIFFERENCES FOR BLOCK 1' This belongs in the calling method. count=0 first_block_differences=[] total=0 while countlen(first_block_estimates): You want to use zip here, like for estimate,actual in zip(block['estimates'], block['actuals']): differences=float(first_block_estimates[count])-float(first_block_times[count]) if differences = 180: differences-=360 elif differences =-180: differences+=360 What if the value is larger than 540 or smaller than -540? total+=differences differences=[differences] This line looks strange. Please use another variable name for values of another type. first_block_differences+=differences If that's all there is to this function, have a look at writing a generator. If you delete the previous line, you can also write jsut first_block_differences.append(differences) count+=1 Not required if you use zip above. average_diff_first_block=total/len(first_block_estimates) text_file.write('\nAverage differences for block 1: ', average_diff_first_block) These two lines don't look as if they belong in this method. Better write another one that wraps the calculation and outputs stuff. def secondBlockDifferences(): Instead of writing two methods to do the same thing, write one with arguments. (...) text_file=open(file_name, 'w') You're leaking the handle. Use with(open(file_name, 'w')) as text_file: . text_file.write('\nParticipant info: ', participant_info) firstBlockDifferences() secondBlockDifferences() - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
[issue15538] Avoid nonstandard s6_addr8
New submission from Philipp Hagemeister: The s6_addr8 field of in6_addr structs is nonstandard, and therefore not supported on all platforms (android in my example). cpython's socket module should use the standard s6_addr field instead. -- components: IO hgrepos: 144 messages: 167233 nosy: phihag priority: normal severity: normal status: open title: Avoid nonstandard s6_addr8 type: compile error versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15538 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15538] Avoid nonstandard s6_addr8
Changes by Philipp Hagemeister phi...@phihag.de: -- keywords: +patch Added file: http://bugs.python.org/file26664/issue15538.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15538 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: Linux shell to python
On 07/30/2012 09:05 AM, Vikas Kumar Choudhary wrote: `lspci | grep Q | grep $isp_str1 | grep $isp_str2 | cut -c1-7' The rough Python equivalent would be import subprocess [ l.partition(' ')[0] # or l[:7], if you want to copy it verbatim for l in subprocess.check_output(['lspci']).splitlines() if 'Q' in l and isp_str1 in l and isp_str2 in l ] You can also just paste the whole pipeline with the shell=True parameter. That's not recommended though, and it's hard to correctly quote strings. - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Linux shell to python
On 07/30/2012 01:31 PM, Jürgen A. Erhard wrote: On Mon, Jul 30, 2012 at 12:35:38PM +0200, Philipp Hagemeister wrote: import subprocess [ l.partition(' ')[0] # or l[:7], if you want to copy it verbatim for l in subprocess.check_output(['lspci']).splitlines() if 'Q' in l and isp_str1 in l and isp_str2 in l ] Ouch. A list comprehension spanning more than one line is bad code pretty much every time. I didn't want to introduce a separate function, but as requested, here's the function version: def pciIds(searchWords=['Q', isp_str1, isp_str2]): for l in subprocess.check_output(['lspci']).splitlines(): if all(sw in l for sw in searchWords): yield l.partition(' ')[0] You could also separate the processing, like this: lines = subprocess.check_output(['lspci']).splitlines() lines = [l for l in lines if 'Q' in l and isp_str1 in l and isp_str2 in l] # Or: lines = filter(lambda l: 'Q' in l and isp_str1 in l and isp_str2 in l, lines) [l.partition(' ')[0] for l in lines] # Or: map(lambda l: l.partition(' ')[0], lines) But personally, I have no problem with three-line list comprehensions. Can you elaborate why the list comprehension version is bad code? Or more to the point, how would *you* write it? - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: catch UnicodeDecodeError
On 07/26/2012 01:15 PM, Stefan Behnel wrote: exits with a UnicodeDecodeError. ... that tells you the exact code line where the error occurred. Which property of a UnicodeDecodeError does include that information? On cPython 2.7 and 3.2, I see only start and end, both of which refer to the number of bytes read so far. I used the followin test script: e = None try: b'a\xc3\xa4\nb\xff0'.decode('utf-8') except UnicodeDecodeError as ude: e = ude print(e.start) # 5 for this input, 3 for the input b'a\nb\xff0' print(dir(e)) But even if you would somehow determine a line number, this would only work if the actual encoding uses 0xa for newline. Most encodings (101 out of 108 applicable ones in cPython 3.2) do include 0x0a in their representation of '\n', but multi-byte encodings routinely include 0x0a bytes in their representation of non-newline characters. Therefore, the most you can do is calculate an upper bound for the line number. - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: catch UnicodeDecodeError
On 07/26/2012 02:24 PM, Stefan Behnel wrote: Read again: *code* line. The OP was apparently failing to see that the error did not originate in the source code lines that he had wrapped with a try-except statement but somewhere else, thus leading to the misguided impression that the exception was not properly caught by the except clause. Oops, over a dozen posts and I still haven't grasped the OP's problem. Sorry! and thanks for noting that. - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: from future import pass_function
Unlike the print statement, pass has no overboarding complexity (like , printing tuples, etc.) - it just serves as a marker (and practicality beats purity). And you don't ever want to use pass as a value (say, for map() or the right side of an assignment). In fact, if pass were a function, users could construct strange code like x = pass() def pass(): raise Exception('Are you slacking off? Get back to work!') And don't forget that while the parentheses mainly confuse users, they're also making it harder to type, and feel like useless overhead (similar to the parentheses in if (x): ). In fact, I'd argue that if pass were a function, None would be the better placeholder: try: do_something() except: None # 2 hard-to-type (on a German keyboard) characters shorter # (and probably way faster: No function call overhead and no need # to actually find out what pass happens to be in this context) - Philipp On 07/25/2012 10:40 AM, Ulrich Eckhardt wrote: Hi! I just had an idea, it occurred to me that the pass statement is pretty similar to the print statement, and similarly to the print() function, there could be a pass() function that does and returns nothing. Example: def pass(): return try: do_something() except: pass() One thing I don't like about this is the syntax class foo(object): pass() What do you think? Uli signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: catch UnicodeDecodeError
Hi Jaroslav, you can catch a UnicodeDecodeError just like any other exception. Can you provide a full example program that shows your problem? This works fine on my system: import sys open('tmp', 'wb').write(b'\xff\xff') try: buf = open('tmp', 'rb').read() buf.decode('utf-8') except UnicodeDecodeError as ude: sys.exit(Found a bad char in file + tmp) Note that you cannot possibly determine the line number if you don't know what encoding the file is in (and what EOL it uses). What you can do is count the number of bytes with the value 10 before ude.start, like this: lineGuess = buf[:ude.start].count(b'\n') + 1 - Philipp On 07/25/2012 01:05 PM, jaroslav.dob...@gmail.com wrote: it doesn't work signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: dict: keys() and values() order guaranteed to be same?
On 07/23/2012 01:23 PM, Henrik Faber wrote: With an arbitrary dictionaty d, are d.keys() and d.values() guaraneed to be in the same order? Yes. From the documentation[1]: If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. In most cases, you should simply use items() though. Can you elaborate on the use case for needing both keys() and values(), where items() is not applicable? - Philipp [1] http://docs.python.org/library/stdtypes.html#dict.items signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Initial nose experience
On 07/15/2012 08:58 PM, Roy Smith wrote: What motivated you to migrate from unittest to nose? Mostly I was just looking for a better way to run our existing tests. We've got a bunch of tests written in standard unittest, but no good way to start at the top of the tree and run them all with a single command. Currently, $ python -m unittest does nothing useful (afaik). Would it break anything to look in . , ./test, ./tests for any files matching test_* , and execute those? - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
unittest: Improve discoverability of discover (Was: Initial nose experience)
On 07/16/2012 01:47 PM, Peter Otten wrote: http://docs.python.org/library/unittest#test-discovery That's precisely it. Can we improve the discoverability of the discover option, for example by making it the default action, or including a message use discover to find test files automatically if there are no arguments? - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest: Improve discoverability of discover (Was: Initial nose experience)
On 07/16/2012 02:37 PM, Philipp Hagemeister wrote: Can we improve the discoverability of the discover option, for example by making it the default action, or including a message use discover to find test files automatically if there are no arguments? Oops, already implemented as of Python 3.2. Sorry, should've checked before. - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: multiprocessing: excepthook not getting called
multiprocessing just mimicks the threading module here, see http://bugs.python.org/issue1230540 . Why do you need excepthook in the first place? You can perfectly simulate it by wrapping the root method (target in your example) in a try .. catch: import multiprocessing import sys def printErrors(func): def wrapped(*args, **kwargs): try: func() except: print ('except: ', sys.exc_info()) return wrapped @printErrors def target(): raise ValueError() if __name__ == '__main__': p = multiprocessing.Process(target=target) p.start() p.join() # try it here in main target() Cheers, Philipp On 06/12/2012 11:02 PM, Dave Cook wrote: Why doesn't my excepthook get called in the child process? import sys import multiprocessing as mp def target(): name = mp.current_process().name def exceptHook(*args): print 'exceptHook:', name, args sys.excepthook = exceptHook raise ValueError if __name__=='__main__': p = mp.Process(target=target) p.start() p.join() # try it here in main target() Thanks, Dave Cook signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: multiprocessing: excepthook not getting called
On 06/13/2012 11:00 AM, Dave Cook wrote: Originally, I was trying to send formatted tracebacks back to the main process on a queue. You can still do that: import multiprocessing import sys def queueErrors(q): def decorator(func): def wrapped(*args, **kwargs): try: func() except: q.put((multiprocessing.current_process().name, repr(sys.exc_info( return wrapped return decorator q = multiprocessing.Queue() @queueErrors(q) def target(): raise ValueError() if __name__ == '__main__': p = multiprocessing.Process(target=target) p.start() p.join() # try it here in main while True: pname,exc = q.get() print('Caught error in process %r: %s' % (pname, exc)) It gets somewhat harder when you also want to catch termination of threads, but you can just queue a special message. - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Any Advice Would Be Greatly Appreciated
If you're looking for skilled developers, the best way to find them is probably to search their current work. http://careers.stackoverflow.com/ and the more experimental http://githire.com/ are two excellent developer-friendly solutions for that. - Philipp On 03/01/2012 12:08 AM, Greg Harezlak wrote: Hello Python Community, I work for a mobile gaming startup in San Francisco, and we're heavily staffing around skilled Python Developers. I've already submitted a job posting to the Python.org website, but I was curious if anyone else had some suggestions on where I could go to find some really awesome people. Thanks in advance for your help. signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
codecs in a chroot / without fs access
I want to forbid my application to access the filesystem. The easiest way seems to be chrooting and droping privileges. However, surprisingly, python loads the codecs from the filesystem on-demand, which makes my program crash: import os os.getuid() 0 os.chroot('/tmp') ''.decode('raw-unicode-escape') Traceback (most recent call last): File stdin, line 1, in module (Interestingly, Python goes looking for the literal file stdin in sys.path. Wonder what happens if I touch /usr/lib/python2.7/dist-packages/stdin). Is there a neat way to solve this problem, i.e. have access to all codecs in a chroot? If not, I'd love to have a function codecs.preload_all() that does what my workaround does: import codecs,glob,os.path encs = [os.path.splitext(os.path.basename(f))[0] for f in glob.glob('/usr/lib/python*/encodings/*.py')] for e in encs: try: codecs.lookup(e) except LookupError: pass # __init__.py or something enumerate /usr/lib/python.*/encodings/*.py and call codecs.lookup for every os.path.splitext(os.path.basename(filename))[0] Dou you see any problem with this design? - Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: youtube-dl: way to deal with the size cap issue + new errors + issues ...
-BEGIN PGP SIGNED MESSAGE- Hash: SHA512 As a general rule, feel free to contact youtube-dl developers and users at https://github.com/rg3/youtube-dl/issues/ . youtube-dl is just one application, which happens to be written in Python. l...@mail.python.org wrote: I did find my way (through a silly hack) to get all files within a size range without waiting for youtube-dl to be enhanced. You may be able to send a HEAD request to the URL, but there's no guarantee the response will contain a Content-Length header. In fact, there will never be a Content-Lenght header for infinite HTTP streams. Also, RTMP URLs are way more complicated. I have also been getting errors reporting: RTMP download detected but rtmpdump could not be run You need rtmpdump. See http://rtmpdump.mplayerhq.hu/ for instructions on how to install it. It would be very helpful if you could redirect youtube-dl errors to a separate file you would indicate via a flag If you think so, please open an issue. Do not forget to consider the usefulness not only for your specific application, but also of other applications. I think the command-line API (https://github.com/rg3/youtube-dl/issues/152) will be your best shot. Note that you can already redirect youtube-dl's output to a file, just like any other shell program: $ youtube-dl uHlDtZ6Oc3s log will write a file log that contains all of youtube-dl's output. If the return code is not 0, an error has occured. Cheers, Philipp youtube-dl developer -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.11 (GNU/Linux) iEYEAREKAAYFAk7KvygACgkQ9eq1gvr7CFw6GwCfeaF0TPNonTCaXVBDnmDBPio2 qVQAn2/JQzTbBYs+pe50t4qVCjxY+BLy =o6uC -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: python-based downloader (youtube-dl) missing critical feature ...
-BEGIN PGP SIGNED MESSAGE- Hash: SHA512 As already said, you should file your request at https://github.com/rg3/youtube-dl/issue , not here. A few things to note: * Not all sites necessarily send the Content-Length header. * RTMP URLs would have to be treated differently * Sending a Range header might allow for a better implementation. Why do you want to restrict the filesize of the download in the first place? I can't see a use case, but that doesn't mean there isn't one. Due to me not having lots of time at the moment, your best chance to get any youtube-dl feature implemented is providing a patch (in form of a github pull-request, if possible). - -- Philipp (youtube-dl developer) l...@mail.python.org wrote: python-based youtube-dl ~ http://rg3.github.com/youtube-dl/ ~ is sorely missing a flag in order to indicate the maximum file length of the data feed it would download (well, unless, for some reason, it is considered a feature). ~ I wonder what developers were thinking about when they came up this nice piece of code. If you actually look in the code ~ ... data = urllib2.urlopen(basic_request) content_length = data.info()['Content-Length'] ... ~ you will see they get the content length of the actual data feed and they also print the progress status based on the content length ~ Implementing an if statement a la: ~ max_indicated_content_length = self.params.get('max_content_length', None); ~ if( content_length max_indicated_content_length ){ [do not download, just report data feed too large] } else{ [do] } ~ shouldn't be hard at all ~ youtube-dl is under the Creative Commons License copyrighted by 2006-2011 Ricardo Garcia Gonzalez and maintained by him and a group of python developers ~ They are the ones keeping a mental map of that project. It would be a plus if they implement this feature, but anyother python developer can implemented (please, let me know if/when you do) ~ lbrtchx -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.11 (GNU/Linux) iEYEAREKAAYFAk6z3mMACgkQ9eq1gvr7CFyr1wCgpqf8xuORDC4LBVY8WFmtAufG k+AAoIX+mXa7SGLULP2M67IQ34sBgk1o =duyH -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: Code Review
Instead of comments, you can often use docstrings (http://www.python.org/dev/peps/pep-0257/ ): This is hard to read due to the indentation, and cannot be accessed programmatically: #Update the GUI def update_gui(self, new_word): Instead, use this: def update_gui(self, new_word): Update the GUI. Now, you can use help(Message) to get information about the method. You'll notice Update the GUI. is not helpfull at all for a method called update_gui. Comments (and docstrings) that reproduce the method name are not useful. A couple of minor things: * If you delete code, delete it, don't comment it out. * Use newlines between two methods. Compare def a(self): pass def b(self): pass def c(self): pass with def a(self): pass def b(self): pass def c(self): pass The latter looks neat and not nearly as crammed as the former. * Don't use newlines where they shouldn't be, for example in if val == 0: label.unbind('Button-1') * Even if it's just the comments, typos make a very bad impression of a coder and the code. I'd automatically assume lots of bugs and untested code when I see more than the occasional typo. * GUI programming is fun, but does not lend itself to structured programming and good practices. You should never use global. Instead, have an object encapsulating the state and pass that object to the method itself or its object. * Don't commit .pyc files, they're totally useless. Since python 2.6, you can add the following in your .bashrc to make python not create them: export PYTHONDONTWRITEBYTECODE=dont In git, you can add the following in your project's .gitignore or ~/.gitignore_global: *.pyc [More on .gitignore: http://help.github.com/ignore-files/ ] * Otherwise, the code looks pretty good for a beginner. You may, however, want to replace def word_not_found(word): if word in searchedwordset: return 0 else: return 1 with just: def word_not_found(word): return word not in searchedwordset (or just skip this method and write word not in searchedwordset). Cheers, Philipp Emeka wrote: Hello All, While learning Python I put together another Text Twist. I would want somebody to go through it and comment. https://github.com/janus/Text-Twist signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: From Python on Solaris to Python on LINUX
What are the traps to be avoided? Assuming you're not using any OS features (scan the code for /dev and /proc), the transition from Solaris to Linux will be seamless. Your main problem will be the transition from the archaic Python 2.3 to a modern one. Luckily, all 2.x Pythons should be backwards-compatible. In summary, your application should work just fine (although being written in 2.3, it's probably not as maintainable as a modern application would). What is the most recent version on LINUX? There are multiple Linux distributions which can differ quite a lot. debian, Ubuntu, and CentOS are popular ones. As you can see on http://www.debian.org/CD/ , the current debian version is 6.0. As you can't see at the moment on http://kernel.org/ , the current Linux kernel version is 3.0 (although most distribution will want to test the kernel and therefore include a slightly older one). -- Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Python3: hex() on arbitrary classes
class X(object): def __int__(self): return 42 def __hex__(self): return '2b' #sic hex(X()) What would you expect? Python2 returns '2b', but python 3(74624) throws TypeError: 'X' object cannot be interpreted as an integer. Why doesn't python convert the object to int before constructing the hex string? Regards, Philipp Hagemeister signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Python3: hex() on arbitrary classes
Mark Dickinson wrote: (...) If you want to be able to interpret instances of X as integers in the various Python contexts that expect integers (e.g., hex(), but also things like list indexing), you should implement the __index__ method: Thanks. Somehow forgot this magic method and deleted it by accident. Philipp Python 3.2a0 (py3k:74624, Sep 1 2009, 16:53:00) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. class X: ... def __index__(self): return 3 ... hex(X()) '0x3' range(10)[X()] 3 'abc' * X() 'abcabcabc' -- Mark signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
[issue6098] xml.dom.minidom incorrectly claims DOM Level 3 conformance
New submission from Philipp Hagemeister phi...@phihag.de: From DOM Level 3[1]: An implementation conforms to a DOM Level 3 module if it supports all the interfaces for that module and the associated semantics.. minidom supports only some Level 3 functions such as Node.renameNode, Element.setIdAttribute*, Text.replaceWholeText and Node.isSameNode. Apart from full DOM Level 2 conformance such as functional namespace handling, many semantics are available under a different name, such as DOMImplementation.getFeature (minidom: getInterface), Text.getWholeText(minidom: _getWholeText) or missing (Attr.schemaTypeInfo, Document.inputEncoding, Document.xmlEncoding, Document.xmlStandalone, Document.xmlVersion, Document.domConfig, Document.adoptNode, Document.normalizeDocument, DOMException.VALIDATION_ERR, DOMException.TYPE_MISMATCH_ERR, Node.baseURI, Node.textContent, Node.compareDocumentPosition, Node.lookupPrefix, Node.isDefaultNamespace, Node.lookupNamespaceURI, Node.isEqualNode, Node.DOCUMENT_POSITION_*, Text.isElementContentWhitespace). Users explicitely asking for DOM Level 3 conformance will run into unexpected problems. Attached is a patch that removes the DOM L3 conformance declarations. -- components: XML files: minidom-dont-declare-level3.diff keywords: patch messages: 88282 nosy: phihag severity: normal status: open title: xml.dom.minidom incorrectly claims DOM Level 3 conformance type: behavior versions: Python 2.7, Python 3.2 Added file: http://bugs.python.org/file14058/minidom-dont-declare-level3.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6098 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: identifying live hosts on a network
We acknowledge your problems with a network script idenifying live hosts on your network. Seriously, you have to tell us a little bit more so that we can help you. How about you actually post the script, and actually post the trouble (What exactly do you expect? What exactly do you see? What other steps have you tried?) Philipp hunteroakes...@gmail.com wrote: HI I am new to python and am having trouble coming up with a script that idenifies all the live hosts on my network. thanks Hunter signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: issue: Permissions in odfpy
Hi Shruti, your message is kind of hard to read. Please note the following: · Do not put useless junk(issue) in title. · Multiple exclamation marks convey a sure sign of a diseased mind, especially syntactically interesting constructions such as ??.. · You didn't purchase your keyboard in August 2001, did you? ( http://ars.userfriendly.org/cartoons/?id=20010820 ) · It's probably just me, but so please give me some solution sounds really rude. Why not any ideas?, thanks in advance or just nothing? Would you answer an email that ended like this? Please keep in mind, you are not paying us, and we're trying to help you just because we want happy python users (or trying to offset us being evil at times). · Everyone mistpyes, and I'd be throwing mountains in my glass house if I complained about grammar, but please show at least some effort. That being said, this might help: import odf.opendocument doc = odf.opendocument.load('in.ods') # You may want to use another way to select the sheet, # this selects just the first one sheet = doc.spreadsheet.childNodes[0] sheet.setAttrNS( 'urn:oasis:names:tc:opendocument:xmlns:table:1.0', 'protected', 'true') doc.save('out.ods') Regards, Philipp shruti surve wrote: hey, i am using odfpy and generating spreadsheet in open office..but nobody should modify the file..so can anybody tell me how can we give permissions (read only) to spreadsheet in odfpy??..i have change the properties of my normal open office spreadsheet file and made it read only..n also extracted it..but in conteny.xml also i am not finding any property which can be used in odfpy...so please give me some solution.. regards shruti surve signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
.pth in current directory: Why doesn't it work as the documentation says?
Where is the fault in my reasoning here? 1) According to http://docs.python.org/dev/install/, The most convenient way is to add a path configuration file to a directory that’s already on Python’s path, (...). 2) Path configuration files have an extension of .pth, (...) 12 = 3) A file test.pth with the content /example/ should result in sys.path containing /example/. 4) (the current directory) is the first element of my sys.path 14 = 5) The current directory qualifies for a directory that’s already on Python’s path, 135 = 6) echo /example/ test.pth; python -c 'import sys;print(repr(sys.path))' should contain '/example'. I think I misinterpreted the documentation, but what exactly is wrong here? Regards, Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: .pth in current directory: Why doesn't it work as the documentation says?
David Lyon wrote: (...) 12 = 3) A file test.pth with the content /example/ should result in sys.path containing /example/. No. Python, once finding the .pth will process it. Yes, but that processing will add /example/ to sys.path, right? 4) (the current directory) is the first element of my sys.path Not always - but maybe it is. Let's not assume anything. Of *my* sys.path. And by default, I think it is. (...) I think I misinterpreted the documentation, but what exactly is wrong here? What are you expecting? and maybe we can tell you why you aren't getting it... David I'm expecting .pth files in the current directory to be be processed, according to docs/install/. Christian Heimes already pointed out this is not the case; so I'm wondering whether this is a mistake in the documentation or just my faulty logic. Cheers, Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: .pth in current directory: Why doesn't it work as the documentation says?
David Lyon wrote: On Mon, 18 May 2009 14:34:33 +0200, Philipp Hagemeister phi...@phihag.de wrote: Yes, but that processing will add /example/ to sys.path, right? It actually works the other way around. The directories listed in sys.path are scanned for .pth files. No, they are not. That's exactly my point. According to Christian Heimes, *some* of the directories listed in sys.path are scanned. is in sys.path, but is not scanned. You can add packages by listing them inside a .PTH. I'm expecting .pth files in the current directory to be be processed, according to docs/install/. Christian Heimes already pointed out this is not the case; so I'm wondering whether this is a mistake in the documentation or just my faulty logic. Perphaps you don't understand them yet. .PTH files are for adding run-time packages. May I ask why you are playing with .PTH files? they are a fairly advanced sort of concept for describing where packages are located to the python interpreter. If you are using .PTH files... you should be using import .. inside your code.. I know that, and I want to use them for an application consisting of multiple scripts, like so: / mylib/ somescripts/ script1.py script2.py mylib.pth (contains '../mylib/') otherscripts/ oscript1.py oscript2.py mylib.pth (contains '../mylib/') If you want, you can change the final point in my original post to: 135 = 6) echo /example/ test.pth;touch /example/libfoo.py; python -c 'import libfoo' Cheers, Philipp signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
[issue6036] test_posixpath.py
New submission from Philipp Hagemeister phi...@phihag.de: At line 300 (version 72674) there is the following interesting construction: if hasattr(os, symlink): if hasattr(os, symlink): (...) (...) hasattr is idempotent. Additionally, test_posixpath.py is missing the shebang and the svn:executable property. -- components: Tests files: test_posixpath-cleanup.diff keywords: patch messages: 87871 nosy: phihag severity: normal status: open title: test_posixpath.py versions: Python 2.7, Python 3.2 Added file: http://bugs.python.org/file13995/test_posixpath-cleanup.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6036 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6036] Clean up test_posixpath.py
Changes by Philipp Hagemeister phi...@phihag.de: -- title: test_posixpath.py - Clean up test_posixpath.py ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6036 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6025] documentation of xml.dom.minidom.parse signature is wrong
Philipp Hagemeister phi...@phihag.de added the comment: The patch has been applied to trunk, but not branches/py3k. Sorry for that, I should really get into the habit of providing patches for both branches. -- status: closed - open ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6025 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6025] documentation of xml.dom.minidom.parse signature is wrong
New submission from Philipp Hagemeister phi...@phihag.de: In the documentation, the signature is given as parse(filename_or_file, parser), although the next paragraph reads (...) *parser*, if given, (...). The latter is correct, and there is a new bufsize parameter, too. -- assignee: georg.brandl components: Documentation, XML files: xml-minidom-parse-signature-doc.diff keywords: patch messages: 87781 nosy: georg.brandl, phihag severity: normal status: open title: documentation of xml.dom.minidom.parse signature is wrong versions: Python 2.5, Python 2.6, Python 2.7, Python 3.0, Python 3.1, Python 3.2 Added file: http://bugs.python.org/file13987/xml-minidom-parse-signature-doc.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6025 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue5379] Multicast example mcast.py is outdated and ugly
Philipp Hagemeister phi...@phihag.de added the comment: Updated patch to use the new ipaddr module instead of the platform-specific socket.inet_pton (unavailable on some platforms, including Windows XP) Updated formatting -- Added file: http://bugs.python.org/file13851/mcast-example.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5379 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue5379] Multicast example mcast.py is outdated and ugly
Changes by Philipp Hagemeister phi...@phihag.de: Removed file: http://bugs.python.org/file13198/mcast-example.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5379 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue5916] Wrong function referenced in documentation of socket.inet_aton
New submission from Philipp Hagemeister phi...@phihag.de: From the documentation of socket.inet_aton (and socket.inet_ntoa): inet_aton() does not support IPv6, and getnameinfo() should be used instead for IPv4/v6 dual stack support. socket.getnameinfo can not replace inet_aton/inet_ntoa at all. However, inet_pton and inet_ntop can (and do support IPv6). Therefore, the attached patch changes the getnameinfo references to inet_pton and inet_ntop. -- assignee: georg.brandl components: Documentation files: aton-doc.diff keywords: patch messages: 87076 nosy: georg.brandl, phihag severity: normal status: open title: Wrong function referenced in documentation of socket.inet_aton type: behavior versions: Python 2.7, Python 3.1 Added file: http://bugs.python.org/file13858/aton-doc.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5916 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue5917] Reference platform-independent alternative in socket.inet_ntop documentation
New submission from Philipp Hagemeister phi...@phihag.de: socket.inet_ntop is only available on some UNIX platforms, although for example Windows users may want to use it. The new ipaddr module provides a platform-independent conversion to the packed format on all platforms and should therefore be used in platform-independent programs. -- assignee: georg.brandl components: Documentation files: doc-packed-ref.diff keywords: patch messages: 87078 nosy: georg.brandl, phihag severity: normal status: open title: Reference platform-independent alternative in socket.inet_ntop documentation type: feature request versions: Python 2.7, Python 3.1 Added file: http://bugs.python.org/file13859/doc-packed-ref.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5917 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: Binary IP address representation
-BEGIN PGP SIGNED MESSAGE- Hash: SHA512 Hi Dave, I've solved this now using ipaddr. ipaddr will be in the stdlib as soon as its developers realize there are actually not one, but two proposals to fix the remaining issues waiting for their input. Anyway, since ipaddr:r68, you can do the following: import ipaddr ipaddr.IP('::1').packed b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01' Since you don't know or want to know the family in most use cases, this interface is actually nicer than that of inet_pton. For the record, here's how my compatibility function looks like. def _compat_ipaddr_inet_pton(family, addr): if family == socket.AF_INET: return ipaddr.IPv4(addr).packed elif family == socket.AF_INET6: return ipaddr.IPv6(addr).packed else: raise ValueError(Unknown protocol family + family) Since socket.AF_INET6 will not be defined on old systems (are there any versions of Python 2.5+ that do not have that defined?), your solution is better for those (but needs netaddr, which is unlikely to enter stdlib soon). Thanks for sharing it. Regards, Philipp DrKJam wrote: Hi, I've only just come across this thread this morning :- http://mail.python.org/pipermail/python-list/2009-March/703388.html Bit late to respond on this list so here is another option (if you are still interested). Try the netaddr.fallback module :- from netaddr.fallback import inet_pton, AF_INET6 inet_pton(AF_INET6, '::1') \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01 It is self contained and written in pure Python. Forms part of the latest 0.6.2 release of netaddr. Apologies the code in my project comes across as unreadable :-( Regards, Dave M. -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (GNU/Linux) iEYEAREKAAYFAknu/zAACgkQ9eq1gvr7CFwUwwCfQLP+dnOdjn9JEttcaFQb5FH0 hLQAn33Lve8k/HXVsW0j7JZP3dL7897W =ki8e -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: how to convert from network to host byte order
-BEGIN PGP SIGNED MESSAGE- Hash: SHA512 Evan wrote: inp='\x04\x00' out = socket.ntohs(struct.unpack('H',inp[:2])[0])) Traceback (most recent call last): File stdin, line 1, in module TypeError: argument 1 must be string or read-only buffer, not int Your approach is nearly right. First of all, you have to tell struct.unpack it should unpack from network order (!): struct.unpack('!H', inp)[0] 1024 Then you want to repack it in host byte order. Use = for that. out = struct.pack('=H', struct.unpack('!H', inp)[0]) out '\x00\x04' For more information, look for Size and alignment in http://docs.python.org/library/struct.html. Regards, Philipp Hagemeister -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (GNU/Linux) iEYEAREKAAYFAkmv2JkACgkQ9eq1gvr7CFymKACghFXMZb9D6pkWZQdapvwTsKJ5 b0UAn0Uvbcguv/rdxjFKXhMQz22+Notn =ZiKx -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
[issue5424] Packed IPaddr conversion tests should be extended
New submission from Philipp Hagemeister phi...@phihag.de: Currently, the testStringToIPv6 and testIPv6ToStrings tests in Lib/test/test_socket.py only check for variants 1 and 2 (but not 3) from RFC 4291 2.2. Furthermore, there are no assertions that check wrong inputs are appropriately refused in any of the packed IP conversion tests. The attached patch adds a number of assertions covering those. -- components: Tests files: extend-packed-ip-tests-trunk2.x.diff keywords: patch messages: 83195 nosy: phihag severity: normal status: open title: Packed IPaddr conversion tests should be extended type: feature request versions: Python 2.7, Python 3.1 Added file: http://bugs.python.org/file13247/extend-packed-ip-tests-trunk2.x.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5424 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Convert IPv6 address to binary representation on 2.x/Windows
-BEGIN PGP SIGNED MESSAGE- Hash: SHA512 socket.inet_pton which does exactly what I want is not available on 2.x on Windows. Strangely, the documentation of socket.inet_aton (IPv4 only) reads: inet_aton() does not support IPv6, and getnameinfo() should be used instead for IPv4/v6 dual stack support. Let's try that: def gni(flag, addr=::1, port=80): ... try: ... res = socket.getnameinfo((addr,port), flag) ... except: ... res = error ... return res ... [(ni,gni(socket.__dict__[ni])) for ni in filter(lambda k:k.startswith(NI_), dir(socket))] [('NI_DGRAM', ('localhost', 'www')), ('NI_MAXHOST', 'error'), ('NI_MAXSERV', ('localhost', 'www')), ('NI_NAMEREQD', ('localhost', 'www')), ('NI_NOFQDN', ('localhost', 'www')), ('NI_NUMERICHOST', ('::1', 'www')), ('NI_NUMERICSERV', ('localhost', '80'))] Neither of these values looks like 0x0001. Am I missing something or is the documentation just wrong? If so, how am I supposed to get a binary representation of an IPv6 address in the absence of socket.inet_pton? Should I write my I own version? -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (GNU/Linux) iEYEAREKAAYFAkmu5N4ACgkQ9eq1gvr7CFwNUgCdF4QdT2LlWWXNlKYbBvKEeTlh lDMAn2eGmFdx7rvM9+gr7tnHlENhgmq7 =Gsal -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert IPv6 address to binary representation on 2.x/Windows
-BEGIN PGP SIGNED MESSAGE- Hash: SHA512 Hi Martin, Martin v. Löwis wrote: I do wonder why you need a binary representation of an IPv6 address... I'd like to subscribe to an IPv6 multicast address via socket.setsockopt(IPPROTO_IPV6, IPV6_JOIN_GROUP, binary_address). Yes, writing your own routine is certainly an option. Is it the preferred one? Because I need this for two purposes: My own code as well as the python multicast example Demo/socket/mcast.py [1]. Alternatively, you can try one of the IP address manipulation libraries, such as ipaddr, or netaddr. Disclaimer: I haven't checked whether these support the requested functionality; please report back when you know. ipaddr[2] has functions that would certainly helpful (int(IP('::1')) yields 1), but not quite the one I'm looking for, although it would be trivial to write it. But then, why should I need ipaddr? netaddr[3] has the function I'm looking for Addr('::1').packed(), but it's way over the top for this purpose; an assembler implementation would be more readable. Kind regards, Philipp Hagemeister [1] http://bugs.python.org/issue5379 [2] http://ipaddr-py.googlecode.com [3] http://code.google.com/p/netaddr/ -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (GNU/Linux) iEYEAREKAAYFAkmu96cACgkQ9eq1gvr7CFzlAwCgrMXI6PVrBGXP5phvv2Fk//9b pQ0An37q6/KNZtIP4OvzYh68NXg4HCU4 =bNqC -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
[issue5379] Multicast example mcast.py is outdated and ugly
New submission from Philipp Hagemeister phi...@phihag.de: The multicast example Demo/sockets/mcast.py 1. mentions that multicast is only implemented on SGI (and optional on other systems). That is not the case anymore. 2. Includes completely unrelated code for broadcast transmission. There is already an example for that, suitably named broadcast.py (in the same directory). 3. Does only support IPv4. 4. Is borderline buggy. Line 85 only works by accident. Similarly, line 37 fails if the TTL would be increased to 127. 5. Is ugly. Line 79-81 are an example of how not to write python code, line 62 can be deleted without changing the program's semantics. (All line numbers refer to rev70006) The attached patch removes broadcast support in mcast.py, adds IPv6 support and fixes all other problems mentioned above. -- components: Demos and Tools files: mcast-example.diff keywords: patch messages: 82810 nosy: phihag severity: normal status: open title: Multicast example mcast.py is outdated and ugly type: behavior versions: Python 2.6, Python 2.7, Python 3.0, Python 3.1 Added file: http://bugs.python.org/file13198/mcast-example.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5379 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com