0

I made in python a little server that listen on port 4444,when i connect with telnet it's all fine,i can send message and the server display the message correctly. I have only one problem: When i try to "parse" the telnet message,I can't do it correctly. Infact with the If output=="screenshot" i want if the message i sent is "screenshot",the server print on the console a message. This don't work. This is my source,i am new to Python and network programming. Thanks in advance ** EVEN IF I USE == IN THE IF STATEMENT IS THE SAME** Coogle

SOURCE

import head
print("SockShot Server Starting\n")
sock=head.Server("localhost",4444)
print("Server STARTED")
while True:
    sock.s.listen(1)
    conn,addr=sock.s.accept()
    print("Connessione stabilita con : {0}:{1} \n".format(addr[0],str(addr[1])))
    conn.send("Benvenuto \n".encode())
    sock.ricevi(conn)

HEAD.py

import socket
import sys
class Server:
    server=""
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    def __init__(self,server,porta):
        self.server=server
        try:
            self.s.bind((server,porta))
            print("Server binding complete \n")
        except:
            print("Binding server incomplete")
            sys.exit(2)

    def ricevi(self,socket):
        result=socket.recv(1024)
        while len(result)>0:
            try:
                print(result.decode())
                result=socket.recv(1024)
                self._check(str(result.decode()))
            except:
                print("Connection CLOSED BY CLIENT \n")
                break;
        socket.close()

    def _check(self,output):
        if output is "screenshot":
            print("Screenshot ESEGUITO \n")
        else:
            print(output)
4
  • could you try to replace print(output) with print("'%s'"%output) to see if there is any invisible char ? Commented Jul 31, 2015 at 14:19
  • Yes,no other chars... Commented Jul 31, 2015 at 14:28
  • And what about len(output) ? should be equal to 10 . Also I see that you are doing result=socket.recv(1024) twice, before the loop and in the loop. Are you sure that your method _check is called ? because there is also print(result.decode()) that print the resut. Commented Jul 31, 2015 at 14:32
  • Yes the function works! I tested with a simple client in python and it send screenshot,and the function parse it correctly! The problem is the message with telnet,i think telnet adds other bytes to the message.. Commented Jul 31, 2015 at 14:40

1 Answer 1

3

One of the possible problem is that you are using is to compare string while you should be using ==:

if output == "screenshot":

== is used to do « standard » comparison (i.e. equality of the value), the one you will use in most cases:

>>> 1 == 1
True
>>> [1, 2] == [1, 2]
True

is is used to check if the two variables point to the same object:

>>> [1, 2] is [1, 2]
False
>>> l1 = [1, 2]
>>> l2 = l2
>>> l1 is l2
True # Same object, if you update l1 you will also update l2
>>> l1[0] = 4
>>> l1, l2
([4, 2], [4, 2])

is may be misleading because in some case it is equivalent to standard ==:

>>> 1 is 1
True
>>> a = 1
>>> b = 1
>>> a is b
True
>>> s = 'hello'
>>> s is 'hello'
True
>>> s = s.lower()
>>> s, s is 'hello'
('hello', False)

So, what going on here?

When you do [1, 2], it creates a new list (new memory block) with value 1 and 2, so if you do [1, 2] twice, two different lists are created, each one with a unique memory location, so [1, 2] is [1, 2] is False.

But, when you type 'hello' you allocate a memory the first time, but then when type 'hello' again, since it is a constant string, it points to the first one (to avoid duplication of identical memory blocks), so s = 'hello' ; s is 'hello' is True. Once you call lower on s, you create a new memory block, and even if this block contains the same data as 'hello', it is not at the same address, so s.lower() is 'hello' is False.

Sign up to request clarification or add additional context in comments.

4 Comments

It's exactly the same i tried is but first i use the ==
So in OP case, he can't use is as output was not defined with the same constant string ?
@Hacketo Yes, in this case is works only if the two variables have been initizalied using the same constant string. But you should not use is to compare the value of two variables.
@Coogle In any way, your code wouldn't work without this answer, even if the problem is elsewhere.

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.