Guide to Python 3
Python 3 guide with just enough basics to begin hacking, note taking from TCM's course
How to read inputs from console
Reading input from STDIN, print output to STDOUT
Sample input
Basic Python 101

Note in the last line than the method "int" ONLY TAKES THE WHOLE NUMBER of a float (etc. returns 29 from 29.9)



Practice Reading Python scripts

Advanced Python 102
Importing, Advanced Strings, Dictionaries
string.split(<insert delimiter>)
<delimiter>.join(string)
string.replace(<stringtoreplace> , newstring)
string.find(<string to find>)

Combining List to Dictionary
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.

Hosting a web server
For example u might wanna do file transfer to a target machine for download


Shitty Port Scanner Script
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:

Sockets
https://realpython.com/python-sockets/#background
Explanation for the Echo Server example shown below
TCP sockets are defined as
socket.SOCK_STREAMUDP sockets are defined as
socket.SOCK-DGRAMs.bind()is used to associate the socket with a specific network interface and port numberHOSTcan be a hostname, IP address, or empty string. If an IP address is used,hostshould be an IPv4-formatted address string. The IP address127.0.0.1is the standard IPv4 address for the loopback 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.listen()enables a server toaccept()connections. It makes it a “listening” socketwe 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 connectionsThis reads whatever data the client sends and echoes it back using
conn.sendall().If
conn.recv()returns an emptybytesobject,b'', then the client closed the connection and the loop is terminated. Thewithstatement is used withconnto automatically close the socket at the end of the block.
Example of a sample implementation : an Echo Server (echo whatever received back to client)
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.
Echo Client
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 prints it.
sys.argv() : Command line arguments 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 command line arguments 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.
For example,

socket.connect(address) -> Connect to a remote socket at address. (The format of address depends on the address family — see above.)
If the connection is interrupted by a signal, the method waits until the connection completes, or raise a socket.timeout on timeout, if the signal handler doesn’t raise an exception and the socket is blocking or has a timeout. For non-blocking sockets, the method raises an InterruptedError exception if the connection is interrupted by a signal (or the exception raised by the signal handler).
Raises an auditing event socket.connect with arguments self, address.
Changed in version 3.5: The method now waits until the connection completes instead of raising an InterruptedError exception if the connection is interrupted by a signal, the signal handler doesn’t raise an exception and the socket is blocking or has a timeout (see the PEP 475 for the rationale).
socket.connect_ex(address) -> Like connect(address), but return an error indicator instead of raising an exception for errors returned by the C-level connect() call (other problems, such as “host not found,” can still raise exceptions). The error indicator is 0 if the operation succeeded, otherwise the value of the errno variable. This is useful to support, for example, asynchronous connects.
Raises an auditing event socket.connect with arguments self, address.
socket.gaierror : reIt means that your given host name ' ' is invalid (gai stands for getaddrinfo() ). A subclass of OSError, this exception is raised for address-related errors by getaddrinfo() and getnameinfo(). 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.
socket.error : just unable to connect
Without KeyboardInterrupt, ctrl + c will cancel only one loop for <try> and move on to next loop and so on. In python, interpreter throws KeyboardInterrupt exception when the user/programmer presses ctrl – c or del key either accidentally or intentionally. KeyboardInterrupt exception inherits the BaseException and similar to the general exceptions in python, it is handled by try except statement in order to stop abrupt exiting of program by interpreter.
Modules (HTTP & FTP Server)
Last updated