Tasks

Python Client & Server

Server

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

port = 12345
host = "127.0.0.1"

s.bind ((host,port))

s.listen(5)

print "Listening..."

while True:

    conn, addr = s.accept()
    print 'Connection from:', addr

    file_name = conn.recv(1024)
    file_name = file_name.strip("\n")
    print "File name:", file_name

    data = conn.recv(2048)
    print "Data Received. "

    file_tmp = open(file_name, "w")
    file_tmp.write(data)
    file_tmp.close()

    print "File created!"
    conn.close

Client

import socket, sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

port = 12345
host = "127.0.0.1"

s.connect((host,port))

file_name = sys.argv[1]
print "File name:", file_name

file_content = sys.argv[2]

s.send(file_name)
print "Send filename."

f = open(file_content, "r")
data = f.read()
s.send(data)
print "Send data."

f.close()
s.close()
print 'uploaded!'

Web Application

HTML

<html>

<head>
    <title> Simple Login Page </title>
</head>

<body>

    <h1> HTML Forms </h1>

    <form method="post" action="login.php">
        Username: <input type="text" name="username"> <br>
        Password: <input type="password" name="password"> <br><br>
        <input type="submit" value="Login">
    </form>

    <p> If you click the "Login" button, the form-data will be sent to a page called "login.php" </p>

</body>

</html>

PHP

<?php

$username = $_POST["username"];
$password = $_POST["password"];

$credentials = array("user1:passwd1", "user2:passwd2", "user3:passwd3");

foreach ($credentials as $credential) {

    $cred_user = explode(":", $credential)[0];
    $cred_pass = explode(":", $credential)[1];

    if ($username == $cred_user && $password == $cred_pass) {
        echo "You have logged in successfully";
        exit();
    }
}

echo "Login failed, please try again.";

?>

Last updated