0

I have the following text file:

ADDRESS1  192.168.124.1
ADDRESS2  192.168.124.2
ADDRESS3  192.168.124.3

And I wrote the following string server in python (strsrv.py) :

#!/usr/bin/env python

import socket
import sys

host = ''
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(backlog)
while 1:
    global f
    client, address = s.accept()
    data = client.recv(size)
    with open('list.txt', 'r') as my_file:
        for f in my_file.readlines():
            if(f.find('%s' % data)>-1):
                data = f
    if f:
        client.send(data)
        client.send(f)
    client.close()

I'm trying to connect to this server sending a string. This string must match one of lines described on text file. Ex: sending 'ADDRESS1' should return 'ADDRESS1 192.168.124.1' from the server, but it doesn't works. Any string sent returns only the last line of the text file. Please could someone point me to the right direction? Thanks :)

1
  • Add a break command after the data = f command. It seems wrong to send both data and f. Commented Apr 19, 2014 at 1:48

1 Answer 1

1

How are you testing this? Assuming you open a socket and connect to the host you should see that you are in fact receiving the correct line as well as the last one. Why? Because in the for loop you keep changing the value of f, the last value of f will be the last line in the file, and you send it back after sending data (which at that point is the correct value).

Here's a suggestion for how you might modify your code (assuming you want the full line back and you dont want wildcarding):

#!/usr/bin/env python

import socket
import sys

host = ''
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(backlog)

# build dict of addresses first, no need to do this for each message
with open('list.txt', 'r') as my_file:
    address_map = dict(line.split() for line in my_file)

while True:
    client, address = s.accept()
    req_address = client.recv(size)
    ip = address_map.get(req_address, 'Not found')
    client.send(req_address + '  ' + ip)
    client.close()

You can simply test this by doing this while the above is running:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('', 50000))
s.send('ADDRESS2')
s.recv(1024)
Sign up to request clarification or add additional context in comments.

4 Comments

I'm testing using netcat to obtain the response from server: echo ADDRESS1 | nc <SERVERADDRESS> <PORT> Assuming the server as localhost, it could be: echo ADDRESS1 | nc localhost 50000
Ah, by the way, thanks for your help. But it doesn't work as well.
You're welcome. This does in fact work though. I had a look at the way you test this, using echo you get a newline character at the end of the packet which is sent to the socket. Basically you are sending 'ADRESS1\n'. Therefore, either do: req_address = req_address.strip() and/or when you try to test this do: echo -n ADDRESS1 | nc localhost 50000
Thank you very much! That '\n' was the little stone. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.