Technical Forum

  Command Line Conversions      Mounting Formats & ISO Creation      Python & Programming Tricks  

         * Python and Programming Tricks *

IP Address Example in C.

//$ gcc -Wall -o myipaddress_c.cgi c_getmyipaddress.c
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>     //for getcwd, chdir & fchdir

#define MAXLINE	200

int main()
    char    *ip, *home;
    char    buff[MAXLINE + 1];

    //Send back HTML with user's message echoed back to the screen
    printf("Content-type: text/html; charset=utf-8\r\n\r\n");
    printf("<html><head><title>IP Address Example in C</title></head><body>\n");

    printf("<h3 align='center'><font color='#aa3300'>IP Address Example in C</font></h3>\n");
    home = getcwd(buff, MAXLINE);
    printf("<p align='center'>HOME: %s </p>\n", home);
    ip = (char *) malloc(MAXLINE);
    ip = getenv("REMOTE_ADDR");
    printf("<p align='center'>Your IP Address: %s </p>\n", ip);

    printf("<br>p align=center><font size=-1>Webmaster:</font></p>\n");
    printf("<p align=center><font size=-1>©, 2018</font></p>\n");
    return 0;

IP Address Example in Python.

#$ pyinstaller
#$ mv dist/p_getmyipaddress/p_getmyipaddress dist/p_getmyipaddress/myipaddress_p.cgi
import os
import sys

print ('Content-type: text/html; charset=utf-8\r\n\r\n')
print ('<html><head><title>IP Address Example in Python</title></head><body>\n')
print ("<br>")
print ("<h3 align='center'><font color='#aa3300'>IP Address Example in Python</font></h3>\n")
print ("<br>\n")
home = os.getcwd()
print ("<p align='center'>HOME: %s </p>\n" % home)
print ("<br>")
ip = os.getenv("REMOTE_ADDR")
print ("<p align='center'>Your IP Address: %s </p>\n" % ip)

print ("<br>")
print ("<br>\n")
print ("<br><p align=center><font size=-1>Webmaster:</font></p>\n")
print ("<p align=center><font size=-1>©, 2019</font></p>\n")
print ("</body></html>)

             *** Forum ***

   Name: cmtstoolbox      <<>>      Date: 2019-07-05

   If the server does not have python installed you have to compile the code with pyinstaller.
   pyinstaller outputs many libraries at compile time, these may work or not depending on the
   program versions the server has.

Back to Main

Webmaster:, 2019