#Python Socket Server Help

1 messages · Page 1 of 1 (latest)

tawny haven
#

imma make this a thread...

crystal lake
#

wat wat wat

tawny haven
#

I made this thread to get help with turning my code into an actual socket server....

crystal lake
#

did you copy the example code?

tawny haven
#

I have this now

crystal lake
#

ok, now you know how to send stuff, and how to receive stuff

tawny haven
#

Will I do the input validation on the client or server side?!

crystal lake
#

dont care about that yet

tawny haven
#

idk how to recieve stuff... you can see b in the output and Hello World is surrounded by '

crystal lake
#

thats a non unicode string

tawny haven
#

what is 1024 about also?

crystal lake
#

thats the buffer, and max size you'll receive

tawny haven
#

oh

crystal lake
#

if you get more you'd need to stick them together, but in your program that wont be an issue

#

unless you plan on sending more than 1024 bytes

tawny haven
#

idk how much text that is xd

crystal lake
#

if ascii, 1024 chars

#

If unicode, encoded as utf8, it depends on the chars used

tawny haven
#

would it be enough for this?

#

7 print statements...

crystal lake
#

you send the data over the socket, not the lines to print

#

client can then print it in a fancy way

tawny haven
#

hmmm

#

ok

crystal lake
#

how do you send it over the wire? thats up to you

#

as long as it is bytes, and that doesnt mean its text :p

#

can be protobuf as well :p

#

(dont do protobuf)

#

not for this.

#

stick to csv or json

tawny haven
#

Uh

#

So I have to send json files from the server??

crystal lake
#

files?

#

bytes

#

and you dont have to

#

you can if you want to

#

as long as you can serialize it on your server and deserialize it on your client it doesnt really matter

#

you can send pickled stuff even if you want

tawny haven
#

Pickled??

crystal lake
#

It's a python persistence thingy, don't use it :p

tawny haven
#

O.o

tawny haven
#

how do I make the server return the menu to ther client and then the client displays it and sends my choice back to server???

#

PROGRESS!!!!!

#

now I need to figure out how to send the choice back to the server and make it call the functions

crystal lake
#

Uhh

tawny haven
#

?

#

yes, 2 and 3 say the same thing, I fixed that

#

they dont do the same thing in the code

crystal lake
#

No.. Client requests data, server sends
But ok
Menu usually isn't part of it hehe

tawny haven
#

he wants a menu ;-;

crystal lake
#

Yeah.

tawny haven
#

so what you mean a menu isnt part of it?

crystal lake
#

When you're done I want to see the code 🙂
Must be creative!

tawny haven
#

so this is the wrong to do this....got it

crystal lake
#

No

tawny haven
#

how do I do it properly?

#

?

#

Is there an easier way?

crystal lake
#

Do what you want, it's your assignment.
Server can send over the options for the client to use

#

And looks like you're doing that

tawny haven
#
import socket

def start_client():
   client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   client_socket.connect(('localhost', 12345))

   menu = client_socket.recv(1024).decode()
   print(menu)

   choice = input("Enter your choice: ")
   client_socket.send(bytes(choice, "utf-8"))

   client_socket.close()

if __name__ == "__main__":
   start_client()
#

this is my client so far....

#

.-.

#

it is sending the choice but idk how to recieve it and use it in the server script

crystal lake
#

Listen to the data again with recv

tawny haven
#

does it send a str by default??

crystal lake
#

No it sends a byte string

tawny haven
#

choice = ch

crystal lake
#

Or rather.. Python gives it to you as a byte string from the recv function

tawny haven
#

o

crystal lake
#

So make it an int

tawny haven
#

thought it sent as bytes?!

crystal lake
#

Well. Yes. But what's that after the rexv

tawny haven
#

.decode()

crystal lake
#

And what does that do

tawny haven
crystal lake
#

You need a debugger
Or a lot of prints

(tip put the thing to print in [])

tawny haven
#

:/

#

how do I close a socket manually without being able to run the script?

#

cant start the server

crystal lake
#

Server is still Running

tawny haven
#

OMFH AI IS SMART

#

it gave me code to put into the server to check if a close.txt exists and to close the socket if it does

#

fuck yeah

crystal lake
#

What do you need that for?

tawny haven
#

to stop the connection that was preventing me from running the server....

crystal lake
#

What other process is trying to open thensocket then your own server?

tawny haven
#

the client

#

but it is a valid choice .-.

crystal lake
#

Why does it not match on the server?

#

Prints

tawny haven
#

huh?

crystal lake
#

There is a if statemenr

#

What is it doing

#

What are you expecting

tawny haven
#
ch_in = client_socket.recv(1024)
        ch = int.from_bytes(ch_in)
        print(ch)

        try:
            if ch < 1 or ch > 4:
                client_socket.send(("\nInvalid choice, Please provide a number between 1 and 3").encode('utf-8'))

            else:
                if ch == 4:
                    print("\n\nThank you for using the HR Control Panel!\n\n")
                    exit()
                else:
                    try:
                        emp_id = input("Enter Employee ID: ")

                        if ch == 1:
                            salary = get_employee_salary(emp_id)
                            client_socket.send(bytes(f"\nThe salary for this Employee is: {salary}"))
                            menu()
                        elif ch == 2:
                            vaycay_days = get_employee_leave_days(emp_id)
                            print(f"\nThis Employee has {vaycay_days} days of Leave Day")
                            menu()
                        elif ch == 3:
                            client_socket.send(bytes(print_employee_details(emp_id)))
                    except ValueError:
                        print("Invalid Employee ID")
                        menu()
        except ValueError:
            client_socket.send(bytes("Invalid choice, Please provide a number"))
            menu()
crystal lake
#

`print([ch])

tawny haven
#

it thinks I sent 49 but I sent 1

crystal lake
#

You sent the ascii value of the char 1

#

Which is..?

tawny haven
#

... oh

#

how do I sent the actual char?

#

.encode('utf-8')

#

🤔 ok lemme see

#

wait...im dumb

#

I need to convert to int

#

🤦‍♀️

#

MORE PROGRESS WOOOOO

#

I have made a discovery... the down arrow on my keyboard doesnt work :/ which is not fun while editing code

#

MORE PROGRESS!!!

#

its a confusing contant back and fourth... had to refactor my function to accept the input before the function call LOL

#

cause I cant start a client socket inside the function itself

crystal lake
#

one way to do it would be to gather info on the client and sent it in one go to the server

tawny haven
#

🤦‍♀️

Oh maybe....
But also idk how to separate all that on the server side

Ooo that would solve an issue I have, 1 out of the 3 functions doesn't ask for further input while the other 2 do
And my client code assumes they will ask for input

#

Gathering it all first would help with that

#

Wait no
I can't send it all in one go to the decree

#

Server*

#

Cause idk what ID or other values the user will want to pass to it

#

Or are you saying mimic the menu on the client side before being run on the server so it asks for the info beforehand??

crystal lake
#

Menu lives entily in the client. Server just accept parameters and returns the answer

tawny haven
#

how though? Menu calls functions that are on the server

crystal lake
#

How do you normally do a http request?

tawny haven
#

uh

#

not sure

tawny haven
#

;-; now im too confused

tawny haven
#

Fuck it, im adding each option to the menu instead of some being in the function

tawny haven
#

Im unsure why the menu prints out just fine, but the logout message doesnt ...

#
menu = client_socket.send(bytes("\n========== HR Control Panel Server ==========" +
                                        "\n\t1. Get Employee Monthly Salary" +
                                        "\n\t2. Get Employee Yearly Salary" +
                                        "\n\t3. Get remaining Employee Vacation Days" +
                                        "\n\t4. Get total Employee Vacation Days" +
                                        "\n\t5. Get all available information about Employee" +
                                        "\n\t6. Exit the Control Panel\n", 
                                        "utf-8"))
client_socket.send(bytes("\n\nThank you for using the HR Control Panel!\n\n", "utf-8"))
tawny haven
#

how do I make it send back the menu instead of quitting? I wanna not have to run the command and make a new connection to the server just to get back to the menu

crystal lake
#

Is that a requirement
? No
Don't bother

tawny haven
#

Yes
It is

tawny haven
#

Finally

#

got the entire menu working over sockets

#

is there a way to format a list on the client side? so it shows what each value in the list means?

#

but also, how do I figure out what function the server is sending the response from?

#

so that I only format the output of this function as a list on the Client

crystal lake
#

the client is just python

#

you can do whatever you want

#

so maybe you need to be able to identify what response you're getting

#

or keep track of what you're requesting

crystal lake
#

send it along with the response

tawny haven
#

huh??

crystal lake
#

make it part of the array

tawny haven
#

added color to the Client output :D

tawny haven
#

so uh

#

how do I put all this into a Docker container?!