sudo apt-get install python sudo apt-get install python3
Ubuntu deliberately packages python3 in such a way it won't interfere with python2.
To run your programs, you'll type "python3" at the command line instead of just "python".
sudo apt-get install python sudo apt-get install python3
d@home$ cat re.py
import re
re_string = "{{(.*?)}}"
some_string = "this is a string with {{words}} embedded in {{curly brackets}} to show an {{example}} of {{regular expression}}"
for match in re.findall(re_string,some_string):
print 'Match->', match
Traceback (most recent call last):
File "re.py", line 1, in <module>
import re
File "/home/nikid/PythonSysadmin/re.py", line 4, in <module>
for match in re.findall(re_string,some_string):
AttributeError: 'module' object has no attribute 'findall'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/apport_python_hook.py", line 48, in apport_excepthook
if not enabled():
File "/usr/lib/python2.6/dist-packages/apport_python_hook.py", line 21, in enabled
import re
File "/home/nikid/PythonSysadmin/re.py", line 4, in <module>
for match in re.findall(re_string,some_string):
AttributeError: 'module' object has no attribute 'findall'
Original exception was:
Traceback (most recent call last):
File "re.py", line 1, in <module>
import re
File "/home/nikid/PythonSysadmin/re.py", line 4, in <module>
for match in re.findall(re_string,some_string):
AttributeError: 'module' object has no attribute 'findall'
NameError: name 'randint' is not defined
This is the site for the book "Learn Python The Hard Way". The book is a very beginner book for people who want to learn to code. If you can already code then the book will probably drive you insane. It's intended for people who have no coding chops to build up their skills before starting a more detailed book.
You can download the book here:
The book is very simple:
Each exercise is one or two pages and follows the exact same format. You type each one in (no copy-paste!), make it run, do the extra credit, and then move on. If you get stuck, at least type it in and skip the extra credit for later.
You might also want to check out these other books if you find this book too boring or annoying:
The book is currently a work in progress, but I'm looking for people to contribute proposed lessons using this wiki. You can also submit tickets with errors you find.
This repository is also available as a fossil repository. Fossil is a distributed version control that includes the wiki, tickets, source, and everything you need to get the whole site. You can use fossil to grab the whole thing and access it offline and contribute to the book.
Use this wiki to start writing your own exercises. You should get the latest version of the book and read it. You should then try the exercises yourself so you get a feel for them.
Next you just write an exercise on the Proposed Exercises page. Just add your exercise as a bullet point with a link and then write it. I leave it to you to figure out how to find the wiki formatting and make the page. If you can't then you probably shouldn't be writing exercises right now.
Finally, make sure your name is on the Exercise so that we know who to blame...credit for its creation.
REFERENCES
http://learnpythonthehardway.org/index
netstat -ntu |awk '{print $5}'| cut -d: -f1 | sort | uniq -c |sort -nr
awk '{print $1}' 10stillhack.txt |cut -d: -f1 | sort | uniq -c |sort -nr|more
def CalculateApacheIpHits(logfile_pathname):
IpHitListing = {}
Contents = open(logfile_pathname, "r").xreadlines( )
for line in Contents:
Ip = line.split(" ")[0]
if 6 < len(Ip) <= 15:
IpHitListing[Ip] = IpHitListing.get(Ip, 0) + 1
return IpHitListing
def TimeSpan(logfile_pathname):
Dates = open(logfile_pathname, "r").readline( )
Last = open(logfile_pathname, "r").readlines( )
firstdate = Dates.split(" ")[3]
lastdate = Last[len(Last)-1].split(" ")[3]
print ""
print "Log covers the dates of: " + firstdate[1:] + " - " + lastdate[1:]
print ""
TimeSpan("access.log.19")
HitsDictionary = CalculateApacheIpHits("access.log.19")
width = 10
ip = "IP Address"
hits = "Hits"
print '%15s %5s' % (ip, hits)
print ' ---------- ----'
for key in HitsDictionary.keys():
print '%15s -> %5s' % (str(key), str(HitsDictionary[key]))
#ps ax -o user,pid |grep 'postfix' |awk '{print $2}' |xargs -l
#ps ax -o user,pid |grep 'postfix' |awk '{print $2}' |xargs -r kill -9
#killall -9 PROCESS
ps -ylC httpd --sort:rss
awk '{print $1}' /var/log/http/access_log | sort |uniq -c |sort -n
netstat -ta |grep ESTABLISHED
sar -w
sar -I SUM
sar -d 5 0
REFERENCES
grep -C 2 "fatal errors" nurseaug3.txt > nurseaug3a.txt
perl -wne'while(/[\w\.\-]+@[\w\.\-]+\w+/g){print "$&\n"}' emails.txt | sort -u > output.txt
find . -name "*.txt" | xargs perl -wne'while(/[\w\.\-]+@[\w\.\-]+\w+/g){print "$&\n"}' emails.txt | sort -u > output.txt
#!/usr/bin/env python
'''
emailsfromfile.py -- Get all unique email addresses from a file
by Patrick Mylund Nielsen
http://patrickmylund.com/projects/emailsfromfile/
License: WTFPL (http://sam.zoy.org/wtfpl/)
'''
__version__ = '1.1'
import sys
import os
import re
import codecs
# Regular expression matching according to RFC 2822 (http://tools.ietf.org/html/rfc2822)
rfc2822_re = r"""(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"""
email_prog = re.compile(rfc2822_re, re.IGNORECASE)
def isEmailAddress(string):
return email_prog.match(string)
def main(filename, separator='\n', encoding=None):
separator_replace = {
'space': ' ',
'newline': '\n',
}
if not os.path.isfile(filename):
raise IOError("%s is not a file." % filename)
results = set()
with codecs.open(filename, 'rb', encoding) as f:
for line in f:
results.update(email_prog.findall(line))
for k, v in separator_replace.iteritems():
separator = separator.replace(k, v)
print(separator.join(results))
if __name__ == '__main__':
args = len(sys.argv) - 1
if 0 < args < 4:
main(*sys.argv[1:])
else:
print("Usage: python %s <filename> [separator] [encoding]" % sys.argv[0])
print("The default separator is a newline. To separate by space, literally enter 'space' as the separator.")
Usage
