09.27.06
Posted in IT, Python, Tutorials at 10:58 pm by Tyler Reguly
Another in my line of tutorial reposts.... I really need to finish up some stuff on this... it was just a small thing I played with... there are problems with it... but it will introduce the basics..
The original (with the code) can be found here
I will also attempt to add the code here in the next day or two.... (I've also got a couple smaller code releases planned)...
Due to errors with code formatting and my inability to get wordpress to display html without rendering it I'm going to attach the tutorial as a text file.
Download the Tutorial
Permalink
Digg this post
09.24.06
Posted in IT, Tutorials at 12:55 pm by Tyler Reguly
Ruby has long been the next language on my list that I'm going to learn... it's always been a matter of finding time... I've finally decided that I'm going to start... so in the future you'll most likely see some basic Ruby stuff from me on this site.... One of the primary sites I'm going to use in my journey is this Learning Ruby site.
It seems to be fairly good at covering all the bases, although it does make the assumption that you have previous programming experience. I'll be reviewing the site as I go through it and learn from it... Also please feel free to share your thoughts and feedback on the site.
Peace,
HT
Permalink
Digg this post
09.21.06
Posted in IT, Python, Tutorials at 1:02 am by Tyler Reguly
Yet another repost... I have nothing new to post at the moment... but all in due time.
Original (Remember even this one is over two years old... the others are three years.... I was still learning when I wrote these... but they're good for introducing the basics)
********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type (indentation lost -- email me if you need help)
code:
"""
Python Script to Deal with Tarballs Flags: c(ompress), d(compress), g(zip)
"""
import tarfile
import string
import sys
try : strFlags = sys.argv[1]
except :
strFlags = "-h"
decompress = "yes"
compress = "yes"
gzip = "yes"
if string.find(strFlags, "-") == 0 :
if string.find(strFlags, "h") != -1 :
print "Usage: %s - " % sys.argv[0]
print "Flags: d[ecompress]n c[ompress]n g[zip]"
sys.exit(0)
try :
strArchive = sys.argv[2]
except :
print "Error Argument Missing"
sys.exit(0)
if string.find(strFlags, "d") == -1 :
decompress = "no"
if string.find(strFlags, "c") == -1 :
compress = "no"
if string.find(strFlags, "g") == -1 :
gzip = "no"
if compress == "yes" and decompress == "yes" :
print "ERROR - CANNOT DECOMPRESS AND COMPRESS"
sys.exit(0)
elif compress == "yes" and gzip == "no" :
try : strFile = sys.argv[3]
except :
print "Error Argument Missing"
sys.exit(0)
tarball = tarfile.open(strArchive, "w")
tarball.add(strFile)
tarball.close()
elif compress == "yes" and gzip == "yes" :
try : strFile = sys.argv[3]
except :
print "Error Argument Missing"
sys.exit(0)
tarball = tarfile.open(strArchive, "w:gz")
tarball.add(strFile)
tarball.close()
elif decompress == "yes" and gzip == "no" :
try:
tarball = tarfile.open(strArchive, "r")
except :
print "ERROR - FILE MISSING"
sys.exit(0)
for tarfile.tarinfo in tarball :
tarball.extract(tarfile.tarinfo)
tarball.close()
elif decompress == "yes" and gzip == "yes" :
try :
tarball = tarfile.open(strArchive, "r:gz")
except :
print "Error - File Missing"
sys.exit(0)
for tarfile.tarinfo in tarball :
tarball.extract(tarfile.tarinfo)
tarball.close()
else :
print "ERROR NO FLAGS GIVEN"
sys.exit(0)
3. Save the script as tar.py
4. Open a command prompt and type python tar.py -h
5. Now for the walk through.
We start off with a comment which is signified by 3 quotation marks
code:
"""
This is also how a comment is ended.
Following this we import the 3 modules we are going to use (tarfile, sys and string). We do so using the import statement. In previous tutorials we used import * from . This was done so that we wouldn't have to reference the module. However I now feel that you can keep up and reference the correct module, this is a more proper way of programming.
I have used a fair amount of error checking in this, so I will cover all those lines right no. While the error checking and the cod are by no means complete, I decided to cover some of it. I mentioned error checking in Introduction to Python #3 if you need to go back and look at it. Basically what it does is it tries to execute the code following try : and if it is successful it carries on with the rest of the program, however if the code fails (if the argument isn't present for example) then it runs the except : code, which prints and error and then uses sys.exit(0) to tell the program to exit cleanly.
Next I set a few variables equal to yes. I suppose I could have used 1/0 but yes/no worked easier for simplicity I wanted. Basically these three variables will store the values of our flags (on or off)
Now we'll check to see if we have a - to signify our flags. The code is slightly redundant here, it has already checked for the present argument and if it didn't exist it set it to -h (the first try and except). This is just making sure the - exists to be picky, if it doesn't the program will exit.
If the flag is set to -h (help) which as you can see makes use of %s to allow us to include the value of a variable in our string, as well as \n which represents a new line (for more info on either of these see Introduction to Python #2 and Introduction to Python #3.
The string.find(strFlags, -) command, simply checks to where the hyphen exists in the strFlags variable. If the - didn't exist a -1 would be returned, since it is in the first position the index of 0 is returned.
Next comes a collection of if statements (I explored if statements in the original Python Introduction
We are now into our tarfile module code. This is what we really want to explore. I have used three options since they will be most recognized, tar, untar, and gzip. The first thing we do every time is open the file we want to work with (this could be creation or an already existing file). We open a file by creating a variable to "store the file" (sorry, I'm a networking guy, not a programmer.. I'm not up on all the lingo).We use the tarfile.open to reference the file.. The first value passed to tarfile.open is the name of the archive we wish to open/create (in this case stored as strArchive), The second value is the mode (r[ead] or w[rite]). If we are dealing with gzip compression we add :gz to tell the module about the compression.
If we are compressing the file, it is rather simple we just access the file by referencing it's variable (tarball) and use the add function, which we pass the name of the file we are compressing, We then close our file stream (Hey I remember the word.. I think.. but i'm not changing it in case I'm wrong) and we close it by referencing the variable/stream (tarball) with the .close function.
If we are decompressing the file, we must decompress once for each file in the tar. We use a For statement (addressed in a previous tutorial i believe), if not it simple says for each file name in this file. We access the module fuction tarfile.tarinfo to find the names of the files in the archive. Then we use that name to extract it using the extract function on the filestream. Passing it the tarinfo function which stores the name of the current file in the archive.
We then close the filestream in the same way we did while compressing a file.
The only thing I didn't touch on was arguments. For you C/C++ programs, this should seem fairly familiar (at least based on my basic knowledge of those languages). sys.argv is an array that stores all the arguments. The first argument would be sys.argv[0], which would be the name of the script being executed, sys.argv[1] would be the name of the first argument following the script name. There is however no sys.argc function, to get the equivalent of argc in C/C++ you would have to use len(sys.argv).
Peace,
HT
Permalink
Digg this post
09.20.06
Posted in IT, Python, Tutorials at 12:57 am by Tyler Reguly
Here's another tutorial repost
Original
***********************
Step-by-Step Process
***********************
1. Open your favourite editor(Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
code:
# Demonstrates various methods of importing modules.
from socket import *
import string
import time
# create a socket of the basic type.
s = socket(AF_INET, SOCK_STREAM)
# define our banner.
senddata1 = "220 desktop Microsoft ESMTP MAIL Service, Version 6.0.2600.1106 ready at" + time.strftime("%a, %d %b %Y %H:%M:%S %Z")
# Query the user for their IP Address and set that and the port
HOST = raw_input("Enter IP Address to bind socket to: ")
PORT = 25 s.bind((HOST, PORT))
# Bind the socket to an IP Address and Port
s.listen(1)
# Have the socket listen for a connection
(incomingsocket, address) = s.accept()
# Accept an incoming connection
incomingsocket.send(senddata1)
# Send our banner
straddress = str(address)
# Convert incoming address to a string
testlist = string.split(straddress, ",")
# Split the tuple into lists
gethost = string.split(testlist[0], "'")
# Split the host portion of the list
getaddr = string.split(testlist[1], ")")
# Split the port portion of the list
host = gethost[1]
# Remove just the address from the list
incomingport = int(getaddr[0])
# Remove just the port from the list
# define our Warning
senddata2 = "Illegal Access of this server, your IP [" + host +"] has been logged."
# Print connection information to the stdout
print "Connection attempt on port", PORT, "from", host, ":", incomingport
# Listen for incoming data
data = incomingsocket.recv(1024)
# Send the Warning
incomingsocket.send(senddata2)
# Close the socket incomingsocket.close
3. Save the script as honeypot.py
4. Open the command prompt and type python honeypot.py (If you get an error, you may already have port 25 in use, simply edit the file to change the port number.)
5. You will be prompted with Enter IP Address to bind socket to: Enter the IP address you wish to have the honey pot listen on. This could be 127.0.0.1 if you simply with to test it, or your outgoing IP if you wish to actually listen for connection attempts.You will now notice nothing, however you can telnet or nc to the IP you entered on port 25. Your connection will display the defined banner 220 desktop Microsoft ESMTP MAIL Service, Version 6.0.2600.1106 ready at followed by the current time (the %
This is only a single connect server and very basic, no complex commands. As time goes on I will post another tutorial on this same honey pot, only expanded to actually convince the user they are connected to the mail server.
The new commands in this tutorial include various socket commands, the time.strftime command and a few string commands.
time.strftime("format") - This command returns a string containing the time and date in the specified format.
str(non-string) - Converts a non-string to a string (there are also int() and tuple() commands).
string.split(string, delimitor) - Splits a string into a list at every delimitor. The list is then referenced by listname[list object number] (numbering begins at 0).
This script contains similar starting socket commands as my first Python Tutorial, however it includes a few extras. After accepting the connection, this script sends the banner using the incomingsocket.send command. This takes the variable we defined on the s.accept (socket accept command) and tells the computer to send data back to it. It then lists for data (incomingsocket.recv) with a maximum buffer size of 1024. Upon recieving this data it again transmits data to the connecting PC and closes the connect with incomingsocket.close.
Source: http://www.python.org/doc/lib/module-time.html
time.strftime format flags
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale's equivalent of either AM or PM.
%S Second as a decimal number [00,61].
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
%x Locale's appropriate date representation.
%X Locale's appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%Z Time zone name (no characters if no time zone exists).
%% A literal "%" character.
Peace,
HT
Permalink
Digg this post
09.18.06
Posted in IT, Python, Tutorials at 7:57 am by Tyler Reguly
The third installment -- originally posted here.
Before I jump into those there is another chart I would like to add. In my last tutorial I mentioned a new way to use the print command, to make it a little more C/C++ like. However I didn't realize that some users may not have C/C++ experience and not know about the % options. So here is a chart with all of your options.
| quote: |
| Source: Python 101 - Beginning Python
d Signed integer decimal.
i Signed integer decimal.
o Unsigned octal.
u Unsigned decimal.
x Unsigned hexidecimal (lowercase).
X Unsigned hexidecimal (uppercase).
e Floating point exponential format (lowercase).
E Floating point exponential format (uppercase).
f Floating point decimal format.
F Floating point decimal format.
g Same as "e" if exponent is greater than -4 or less than precision, "f" otherwise.
G Same as "E" if exponent is greater than -4 or less than precision, "F" otherwise.
c Single character (accepts integer or single character string).
r String (converts any python object using repr()).
s String (converts any python object using str()).
% No argument is converted, results in a "%" character in the result.
|
Now on to our new lesson.
I would like to look at executing files located on your system. As I mentioned in the first tutorial I would like to keep this OS inspecific so we will deal with the ping command as it is universal. I will be using the count flag and I will use the windows flag which is -n, so *nix users must remember to change it to -c or they will get an error.
********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
code:
from os import *
count = input("Number of times to ping host: ")
host = raw_input("IP of host to ping: ")
pingcmd = "ping -n %i %s" % (count, host)
execute = popen(pingcmd)
results = execute.readlines()
execute.close()
length = len(results)
for x in range (length) :
print results[x]
3. Save the script as pingtest.py
4. Open a command prompt and type python pingtest.py
5. This one is a little more difficult to walk you threw. First you will see Number of times to ping host: . Enter a number and press enter. Then you will see IP of host to ping: . Enter an IP and again press enter. There will be a pause (the length of which will depend on how many times you told it to ping). Then you will see the standard output of a ping command written to the screen.
Now to walk you threw this script. We import the OS module and then query the user for the number of pings and the host to ping. Then we define our ping command using the same format as the print command. The next line actually executes the command followed by a line which reads the output of the command into a variable. We then close the variable which executed the command. The next line introduces another new command the len command, which returns the length. In this case since results in an array it returns the number of lines in the array. We then make use of the for command we learned in our last tutorial and have it print the actual content of the array, line by line. Notice the use of the whitespace again for the for command.
Alrighty.... moving on, I believe I promised you file reading/writing next. We'll work with a simple txt file.
********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
code:
print "File Write/Create Example."
filename = raw_input("File to Write To/Create: ")
openfile = file(filename, 'w')
print "Enter text to write to file (Type 'quit' to quit):"
line = ""
alllines = ""
while 1 :
line = raw_input()
if line == 'quit' :
break
alllines = alllines + line + "n"
file.write(openfile, alllines)
file.close(openfile)
print "You just wrote to a file, Now Let's see what you wrote."
print "File Read Example."
openfile = file(filename, 'r')
filedata = openfile.readlines()
length = len(filedata)
for x in range(length) :
print filedata[x]
openfile.close
3. Save the script as filetest.py
4. Open a command prompt and type python filetest.py
5. When you run this program you will be prompted to enter a filename. Call the file anything you want. The next line of code then open's that file in write ('w') mode (will create it if it doesn't exist). The code then tells you to type your text, and declares to blank variables. The while 1 : is just a way of creating an infinite loop, so that every time the user hits enter, it moves to a new line of the file. The line is used to receive input, but we aren't prompting the user each time. The if statement introduces another new command, break. This will exit the loop upon seeing the string 'quit'. The next line of code simply concatenates the previous lines with the new line and then adds a newline character. After the user has entered quit we write to the file and close the file. You then see a few lines of text. The script them opens the file in read ('r') mode. We read the lines into an array and determine the number of lines like we did in our ping tutorial. We then enter a fore statement and print each line until we get to the end of the file. Then we close the file.
The last thing I promised for this tutorial was error handling. This is useful for small things like if you've forgotten to declare a variable and other such events. I can't think of a useful example at the moment, so I'll just go over the commands and if I think of some code later I'll add it.
Basically the commands are try and except.
Instead of just issuing the code, you try it. For example if you were attempting to read a file that didn't exist. The program would exit and return an error. However if you try and have an except...
code:
try :
except :
print "File doesn't exist."
Now if the file doesn't exist it will simple print File doesn't exist and continue on with the program.
Peace,
HT
Permalink
Digg this post
09.17.06
Posted in IT, Python, Tutorials at 7:46 am by Tyler Reguly
The second in my line of imported tutorials... This was originally posted here.
This tutorial will assume you have already read my first Python Tutorial (Python Introduction).
I'm not real sure yet what I'm going to cover, I figure I'll pick at these for a couple of days and post it at the end of the weekend. Unless I get really ambitious tonight.
Before I get into some examples, I'm going to include a few charts
| quote: |
| Source: Python Reference Manual - 2.4.1 String Literals
Escape Sequences
\newline Ignored
\\ Backslash (\)
' Single quote (')
\" Double quote (")
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\N{name} Character named name in the Unicode database (Unicode only)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\uxxxx Character with 16-bit hex value xxxx (Unicode only)
\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (Unicode only)
\v ASCII Vertical Tab (VT)
\ooo ASCII character with octal value ooo
\xhh ASCII character with hex value hh
|
| quote: |
| Python Operators
+ addition
- subtraction
* multiplication
/ division
% module
** To the Power Of
& Bitwise And
| Bitwise Or
== equals
!= Does not Equal
<> Does not Equal (Outdated)
< Less Than
> Greater Than
< = Less Than or Equal To
>= Greater Than or Equal To
not Logical Negation
and Logical And
is, is not, in, not in Comparison
|
Now for some examples..
Let's start with the while statement.
********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
code:
from random import *
answer = randint(1,25)
guess = 0
counter = 0
print "Welcome to the Number Guessing Game"
while guess != answer :
guess = input("Please Guess a Number between 1 and 25: ")
if guess < answer :
print "To low."
elif guess > answer :
print "To high."
counter = counter + 1
print "Congrats you guessed the correct number (",answer,") in",counter,"tries."
3. Save the script as numberguess.py
4. Open a command prompt and type python numberguess.py
5. You will see Welcome to the Number Guessing Game, followed by a new line which states Please Guess a Number between 1 and 25. I can't walk you though this, since it uses a random number but as you guess you will see To low. or To high. Until you guess the correct number, when it will then display Congrats you guessed the correct number ( ) in
tries.
This program incorporates a lot of firsts. We've made use of the Random module which allows us to call the command randint(bottom number, top number) to randomly generate an integer. We've created a few additional variables to store values and entered into a While Statement. This is also the first time we've looked at white space. Python makes use of white space quite frequently. As you notice there is no End while or End if statements, instead the tabs (white space) tell the interpreter where each statement ends.
The while statement follows the format while : .
This next example will be relatively small as I just want to introduce the for statement and nothing else.
********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
code:
print "This program will list the first 10 powers of a number."
usednum = input("Enter the number to use: ")
for x in range(10) :
print usednum, "^", x, "=", usednum ** x
3. Save the script as powers.py
4. Open a command prompt and type python powers.py
5. You will see This program will list the first 10 powers of a number.. Followed by Enter the number to use: . For this example, enter the number 2 and press enter. You will then see.
2 ^ 0 = 1
2 ^ 1 = 2
2 ^ 2 = 4
2 ^ 3 = 8
2 ^ 4 = 16
2 ^ 5 = 32
2 ^ 6 = 64
2 ^ 7 = 128
2 ^ 8 = 256
2 ^ 9 = 512
This for statement uses and undeclared variable (which initializes to 0) and then cycles threw 10 times, leaving us with a last value of 9. Each time threw the cycle it prints the resulting power.
Many of you who have used C/C++ in the past have probably noticed a few similarities and this is because they exist. I have already shown you the print statement, but I will now show you another way to accomplish the same thing that is closer to the way C/C++ works.
********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
code:
name = raw_input("What is your name: ")
print "Your name is %s?" % name
3. Save the script as print.py
4. Open a command prompt and type python print.py
5. You will see a prompt which reads What is your name: . After Entering your name and pressing enter. You will see Your Name is ?.
This is a great method for joining text and variables because it doesn't add a single space like the comma did in past print statements.
Peace,
HT
Permalink
Digg this post
09.16.06
Posted in IT, Python, Tutorials at 7:46 am by Tyler Reguly
I've been thinking about it quite a bit lately, and I've decided to repost some of my older articles and tutorials written for ther sites.. Some of this stuff is rather basic... but it could prove useful for a number of peole out there...
This first article is a very simply python introduction that I wrote for the site AntiOnline.com. The original posting can be found here.
I'm going to pass on describing Python to you and instead let Webopedia do it for me.
| quote: |
| Source: Webopedia Term: PythonAn interpreted, object-oriented programming language developed by Guido van Rossum. The name comes from one of van Rossum's favorite television shows, Monty Python's Flying Circus. Python is very portable since Python interpreters are available for most operating system platforms. Although Python is copyrighted, the source code is open source, and unlike GNU software, it can be commercially re-sold. |
For those of you who do not have a Python interpreter, they are available on the net for download
Python.org Download Page
ActivePython from ActiveState
Now for the tutorial.
I'll try and make this as OS inspecific as possible. All these walk-throughs assume you have installed Python already.
Let's start with the most basic of programs. The obvious Hello World! Program.
********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
code:
print "Hello World!"
3. Save the script as helloworld.py
4. Open a command prompt and type python helloworld.py
5. Voila! Hello World! is displayed on the screen.
This simply demonstrates the print command in Python. Now let's look at variables and user input.
********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
code:
age = input("What is your age: ")
print "So you are",age,"?"
3. Save the script as age.py
4. Open a command prompt and type python age.py
5. You will see an prompt which says What is your age:. Enter your age and press return. You will now see So you are 21 ?.
You will notice you do not have to declare variables in Python, simply reference them. In this example you also see how the input command is used. To input a value simply define a variable name for the value and then set it equal to the input command. The structure of the input command is
code:
input("Question to prompt with")
. You also see how to output more than one string of data, using the , to concatenate the strings. However use of the comma also automatically inserts a space. You MUST remember that input only works with numbers
Now we will attempt a simple text calculator with 4 commands (add, subtract, multiply and divide).
********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
code:
calc = raw_input("Which function would you like to execute - [a]dd, [s]ubtract, [d]ivide, [m]ultiply: ")
firstnum = input("Enter the first number: ")
secondnum = input("Enter the second number: ")
if calc == "a" : print firstnum,"+",secondnum,"=",firstnum+secondnum
elif calc == "s" : print firstnum,"-",secondnum,"=",firstnum-secondnum
elif calc == "d" : print firstnum,"/",secondnum,"=",firstnum/secondnum
elif calc == "m" : print firstnum,"*",secondnum,"=",firstnum*secondnum
else : print "Error: Unknown Function Entered"
3. Save the script as calc.py
4. Open a command prompt and type python calc.py
5. You will see a prompt which says Which function would you like to execute - [a]dd, [s]ubtract, [d]ivide, [m]ultiply: . For the sake of demonstration enter a and press enter. You will then see Enter the first number: . Enter 5 and press enter. Now Enter the second number: " appears. Enter 7 and hit enter. The screen will now display 5 + 7 = 12. Feel free to experiment with other functions and numbers now.
You have now been introduced to the command raw_input. It fuctions the same way as input but is used for alphanumeric entries. You are also introduced to basic mathematical functions in python (+,-,/,*) and if statements. The syntax for an if statement is
code:
if (statement) : elif (statement) : else :
One last example. We will attempt to incorporate a basic introduction to sockets into this example. *Crosses his fingers*
********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
code:
from socket import *
s= socket(AF_INET, SOCK_STREAM)
yourip = raw_input("Enter your IP Address: ")
yourport = input("Enter a Port Number to Listen on: ")
s.bind((yourip, yourport))
s.listen(1)
(incomingport, incomingaddress) = s.accept()
print "Connection Attempt from", incomingaddress
s.close
3. Save the script as server.py
4. Open a command prompt and type python server.py
5. You will see a prompt which reads Enter your IP Address: . Do as it says and press enter. You will then see a prompt which says Enter a Port Number to Listen on: . Answer the question and press enter. Telnet to the port you entered and wait a few seconds. Close the session and return to your python window. You will see the text Connection Attempt from .
In this example we played with sockets a little bit. The first task was to include the socket module. We did this using the statement
code:
from socket import *
. We then defined the type of socket (AF_INET, SOCK_STEAM) and created a variable for the socket to be attached to (s). We could then issue our commands based on the varible s. We bound the socket to the ip and port you had previously entered with the bind command and then started listening on that port. Upon a connection attempt we accepted the connection, printed the intruders IP to the screen and closed the socket.
Peace,
HT
Permalink
Digg this post
03.16.06
Posted in IT, Tutorials at 3:44 am by Tyler Reguly
This is old news to those of you that follow AntiOnline.com and Irongeek.com that come here as well, and will be exciting for those of you that are attending Notacon, as Irongeek will be presenting this information there. Anyways... he has a really great tutorial on hacking network printers on his website @ http://www.irongeek.com/i.php?page=security/networkprinterhacking. Also he had a test example up using a php page @ http://tux.ius.edu/printer.php, however since being posted on digg.com he has had to take it offline because it was being hammered, check out the video to se how fast it was going.
Peace,
HT
Permalink
Digg this post
03.09.06
Posted in IT, Tutorials at 12:26 am by Tyler Reguly
So I do some programming... not a lot but enough to get by... I like networking, I like operating systems... I like getting my hands dirty... to me programming has always been something done by people that can't use a computer in it's entirety... This belief is slowly starting to change... so I've decided to look further into the subject. To start... I looked into Little Endian and Big Endian numbers... It's really as simple as it sounds.. (unless someone replies and tells me I'm a moron). With little endian, the little number goes in the lowest address space.. so Ones, then 10s, then Hundreds (if you're talking decimal). With big endian, the big number goes in the lowest address space.. Hundreds, than 10s, then ones... Little Endian = PC, Big Endian = Motorola (Pre-Intel Macs)... now I'm curious about the Intel Macs.. If anyone knows and wants to save me a simple google search.. feel free to contribute.. otherwise I'll do some googling..
Anyways.. I relied on this page which proved very interesting... Check it out http://www.cs.umass.edu/~verts/cs32/endian.html.
Peace,
HT
Permalink
Digg this post