Guide to Python 3
Python 3 guide with just enough basics to begin hacking, note taking from TCM's course
Last updated
Python 3 guide with just enough basics to begin hacking, note taking from TCM's course
Last updated
Reading input from STDIN, print output to STDOUT
Sample input
Note in the last line than the method "int" ONLY TAKES THE WHOLE NUMBER of a float (etc. returns 29 from 29.9)
Importing, Advanced Strings, Dictionaries
string.split(<insert delimiter>)
<delimiter>.join(string)
string.replace(<stringtoreplace> , newstring)
string.find(<string to find>)
The Python zip() function accepts iterable items and merges them into a single tuple. The resultant value is a zip object that stores pairs of iterables. You can pass lists, tuples, sets, or dictionaries through the zip() function.
For example u might wanna do file transfer to a target machine for download
A good ol homemade nmap scanner to scan for open ports, for this script we are scanning only 50 to 85 as it takes too long!
results:
Explanation for the Echo Server example shown below
TCP sockets are defined as socket.SOCK_STREAM
UDP sockets are defined as socket.SOCK-DGRAM
s.bind()
is used to associate the socket with a specific network interface and port number
listen()
enables a server to accept()
connections. It makes it a “listening” socket
we now have a new socket object from accept()
. This is important since it’s the socket that you’ll use to communicate with the client. It’s distinct from the listening socket that the server is using to accept new connections
This reads whatever data the client sends and echoes it back using conn.sendall()
.
the API calls the server makes to setup a “listening” socket:
socket()
bind()
listen()
accept()
A listening socket does just what it sounds like. It listens for connections from clients. When a client connects, the server calls accept()
to accept, or complete, the connection.
For example,
AF_INET
is the Internet address family for . SOCK_STREAM
is the socket type for , the protocol that will be used to transport our messages in the network.
HOST
can be a hostname, , or empty string. If an IP address is used, host
should be an IPv4-formatted address string. The IP address 127.0.0.1
is the standard IPv4 address for the interface, so only processes on the host will be able to connect to the server. If you pass an empty string, the server will accept connections on all available IPv4 interfaces.
If conn.recv()
returns an empty object, b''
, then the client closed the connection and the loop is terminated. The is used with conn
to automatically close the socket at the end of the block.
It creates a socket object, connects to the server and calls s.sendall()
to send its message. Lastly, it calls s.recv()
to read the server’s reply and then .
sys.argv() : are those values that are passed during calling of program along with the calling statement. Thus, the first element of the array sys.argv()
is the name of the program itself. sys.argv()
is an array for in Python. To employ this module named “sys
” is used. sys.argv
is similar to an array and the values are also retrieved like Python array.
socket.gaierror : reIt means that your given host name ' ' is invalid (gai stands for getaddrinfo() ). A subclass of , this exception is raised for address-related errors by and . The accompanying value is a pair (error, string)
representing an error returned by a library call. string represents the description of error, as returned by the gai_strerror()
C function.