Thread: Comparing Python strings to C strings

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    15

    Comparing Python strings to C strings

    I have two programs, a server (written in C) and a client (Python). I'm trying to set up a simple login system where the client enters a username and password, which are sent to the server via socket connection, and then the server compares the received strings with its stored credentials. I have tried every possible type of string comparison/manipulation I can think of to get a legitimate comparison of the characters in the strings, but it is still saying "password" does not equal "password" etc.

    Client (Python):
    Code:
    print "Username: ",
    username = sys.stdin.readline() # Enters "user"
    s.send(username)
    
    password = getpass.getpass() # Enters "password"
    s.send(password)
    And on the server side:
    Code:
    // Hardcoded credentials (for now)
    char *username = "user";
    char *password = "password";
    ...
    // Get username from client
    bzero(recvbuf, BUFSIZE); // Clear the string buffer for received messages
    n = read(acceptfd, recvbuf, BUFSIZE); // Read any incoming messages
    
    cliUsr = recvbuf;
    printf("Username: %s", cliUsr); // Prints out "user"
    fflush(stdout); // Flush the output buffer
    
    // Get password from client
    bzero(recvbuf, BUFSIZE); // Clear the string buffer for received messages
    n = read(acceptfd, recvbuf, BUFSIZE); // Read any incoming messages
    
    cliPw = recvbuf;
    printf("Password: %s\n", cliPw); // Prints out "password"
    fflush(stdout); // Flush the output buffer
    
    if (strcmp(cliUsr, username) == 0 && strcmp(cliPw, password) == 0) {
        accessGranted == true;
    }


    I am going crazy and any help is appreciated. Thanks!

    Edit: I changed
    sys.stdin.readline() to raw_input() and am able to correctly compare strings with C.
    Last edited by hinesro; 11-29-2015 at 10:00 PM. Reason: solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing strings in c
    By adel in forum C Programming
    Replies: 17
    Last Post: 02-14-2010, 01:33 PM
  2. need help comparing strings
    By busdude in forum C Programming
    Replies: 2
    Last Post: 12-02-2009, 09:00 PM
  3. [HELP] about comparing between two strings...
    By kyaky in forum C++ Programming
    Replies: 12
    Last Post: 08-06-2007, 10:12 AM
  4. comparing strings
    By jrahhali in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2003, 08:41 PM
  5. comparing strings
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-23-2002, 11:20 AM