Search code examples
phppythontcpfile-writing

"Delayed" writing to file in python


I have very specific problem and I have no idea how to get rid of it. I'm sending some data over php client on webserver. Code looks like this:

<?php
session_start();

include 'db_con.php';
include('db_functions.php');

error_reporting(E_ALL);

/* Get the port for the WWW service. */
$service_port = $_SESSION['port'];

/* Get the IP address for the target host. */
$address = $_SESSION['ip'];

/* Create a TCP/IP socket. */
try {
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
} catch (Exception $e) {
    echo "error" . socket_strerror(socket_last_error()) . "\n";
socket_close($socket);
}

try {
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

} catch (Exception $e) {
    echo "error" . socket_strerror(socket_last_error()) . "\n";
    socket_close($socket);
}

$result = socket_connect($socket, $address, $service_port);

//data from database soonTM
$in = returnResults($db);

$data = "";
foreach ($in as $r){
    $data .= $r['ring_time'] . ' ' . $r['bell_mode'] . "\r\n";
}

socket_write($socket, $data, strlen($data));

echo "Reading response:\n\n"; 
while ($out = socket_read($socket, 2048)) {
    echo $out;
}

socket_close($socket);
?>

Data from database is then sent to TCP server and it Works great, since I get the exact same data as I sent.

Anyways this is my TCP server code:

import socket

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('10.10.10.120', 10000)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)



while True:
# Listen for incoming connections
sock.listen(1)
# Wait for a connection
print('waiting for a connection')
connection, client_address = sock.accept()

try:
    print('connection from', client_address)

    # Receive the data in small chunks and retransmit it
    while True:
        data = connection.recv(2048)
        neki = 'hello rok'
        print('received "%s"' % data)
        if data:
            try:
                dataFile = open('data.txt', 'w')

                try:
                    dataFile.write(data)
                except (Exception) as inst:
                    print(type(inst))
                    print("error at writing to file")
            except (Exception) as inst:
                print(type(inst))
                print("error opening file")
            print('sending data back to the client')
            #connection.sendall(bytes(neki, 'UTF-8'))
            connection.sendall(neki)
            break
except (Exception) as inst:
    print(type(inst))
    print("error opening connection")

finally:
    # Clean up the connection
    connection.close()

And it seems to be working perfectly since in terminal it prints the stuff I sent via TCP client. However, the problem is with writing to data.txt file. When I send data for the first time, nothing Will be written to the file. If I send it again it Will work as intended (even though the right data is printed in terminal). If I add another line to my database and send it again, the old value Will be written to file again. If I send it again, the new value Will be added.


Solution

  • You need to close your file. I think the problem in this. Please try to add 'finally' block like this:

           try:
                dataFile = open('data.txt', 'w')
    
                try:
                    dataFile.write(data)
                except (Exception) as inst:
                    print(type(inst))
                    print("error at writing to file")
            except (Exception) as inst:
                print(type(inst))
                print("error opening file")
            finally:
                dataFile.close()