Reputation: 23
I have a simple php script (that runs on xampp) for long polling server, but I would like to translate it to Python and make it a Python server. I'm kind off inexperienced with python (specially when it comes to web servers) and I haven't found any simple solutions for Python web servers that would do this same thing so I'm wondering, could anyone help me translating this script from php to Python.
My php script does this: it GETs timestamp from the client and then keeps comparing it to current modification time of file, and if they are different, it sends contents of file and new timestamp coded in JSON. Here is my code:
<?php
$filename = dirname(__FILE__).'/data.txt';
$lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$currentmodif = filemtime($filename);
while ($currentmodif <= $lastmodif){
usleep(10000);
clearstatcache();
$currentmodif = filemtime($filename);
}
$response = array();
$response['msg'] = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
?>
Upvotes: 1
Views: 345
Reputation: 10450
The following links include a sample of simple python server, and some additional information, it should give you a good starting point for python tcp/ip client/server programming:
TCP/IP Client and Server - https://pymotw.com/2/socket/tcp.html
python wiki - TCP Communication - https://wiki.python.org/moin/TcpCommunication
Python docs - 20.17.4.1. SocketServer.TCPServer Example - https://docs.python.org/2/library/socketserver.html#SocketServer.TCPServer
Upvotes: 1