Skip to content

Instantly share code, notes, and snippets.

@4anonz
Created August 3, 2021 17:10
Show Gist options
  • Save 4anonz/6909368aa110f2e1454cf04e18e26a7f to your computer and use it in GitHub Desktop.
Save 4anonz/6909368aa110f2e1454cf04e18e26a7f to your computer and use it in GitHub Desktop.
Simple server program in python
#/usr/bin/python3
# Simple implemenation of server in python
import socket
from os import system
def main():
print("++++++[*]Starting server")
#Leave it empty to use any available interface on the machine
host = ""
#port number to listen on
port = 4444
print("++++++[*]Creating socket...")
#create a server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("++++++[!]Socket created successfully")
#bind the newly created server socket to hostname and port number
print("++++++[*]Binding socket to hostname and port number...")
try:
server_socket.bind((host, port))
except socket.error as err_msg:
print(f"++++++[-]Failed to bind socket. Error ({str(err_msg)})")
system("exit(1)")
print("++++++[!]Bind finished")
#listen for connections on the specificied port number
server_socket.listen(10)
print(f"++++++[*]Server Listening on port {port}")
print("++++++[*]Waiting for connections...")
#Accept connection if available
while 1:
#accept() returns a new socekt for the session and the client address
#we store that in a varaible
client_socket, address = server_socket.accept()
print(f"++++++[*]Got a new connection from {str(address)}")
server_socket.close()
if __name__ == '__main__':
system("clear")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment