Code a simple socket server in Python Code a simple socket server in Python 26 Comments Python sockets In a previous tutorial we learnt how to do basic socket programming in python.
It can be used to present web-based interfaces on the Pi and is relatively easy to setup. This tutorial will explain how to create a basic site to get you started. I use Python 3 in this tutorial.
Create a fresh SD card To avoid conflicts with other software you may have installed I would recommend starting off with a fresh SD card by writing the latest Raspbian image to it. If you want to configure the Pi over the network from another computer it can be enabled by either: If you are using a monitor and keyboard you can run: When writing this tutorial my Pi was using If you are connecting remotely via SSH then you can use an IP scanner to find it or it will listed somewhere in your router settings.
Set the new password and then run: To ensure we will be installing the latest packages run the following two commands: Install pip Before we can install Flask we need to install pip, the Python package manager: Create test Flask app Now that Flask is installed we need to create a small test site to check everything is working.
Create a new folder: This script defines a simple one page website. Testing the Python web server You can now run the script using: Note that Flask uses port by default and you need to replace Take a look at the example below: This page will be displayed when you visit the hello sub-directory: Even more routes You can also pull information from the URL into your script to create more elaborate page combinations.
This makes the script much easier to handle when your pages are a bit more complicated. Create a new directory for templates: So the visitor sees a slightly different page. The great thing with templates is that they keep the main Python script focused on functionality and leave the layout and aesthetics to the template file.
This runs Flask in debug mode which automatically reloads Flask when you update the script.
It also provides error messages if the page fails to load. If you expose the site to the internet the debug flag should be set to False.
Auto-running Script on Boot If you want the Python script to automatically run when the Pi boots you can use this technique: Insert the following line at the bottom of the comments block: This means when the Pi boots it will automatically login as the Pi user.
Download Scripts The example scripts and templates in this tutorial are available in my BitBucket repository.My Pi Web server hosts a single Web page that connects to a MySQL database, which in turn gets its data from a Python program, which in turn is getting data from a smart thermometer—and all of.
Mar 11, · If you are running SQL Server on the same PC where you are running your Python code the server name will be localhost Database - is the name of your database in SQL Server. I have created a database called testdb.
The goal of this assignment is to learn the basic of socket programming in Python using TCP. Web Client Write an HTTP client program that can retrieve a single page from a web server.
The program takes three command-line arguments. Mar 04, · In this video we'll be creating a really simple web server in Python using the Python http library.
This is not a tutorial in any specific language, but serves as a guide to facilitate writing your own server.