#Python Socket Server Help
1 messages · Page 1 of 1 (latest)
wat wat wat
I made this thread to get help with turning my code into an actual socket server....
did you copy the example code?
ok, now you know how to send stuff, and how to receive stuff
Will I do the input validation on the client or server side?!
dont care about that yet
idk how to recieve stuff... you can see b in the output and Hello World is surrounded by '
thats a non unicode string
thats the buffer, and max size you'll receive
oh
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
idk how much text that is xd
you send the data over the socket, not the lines to print
client can then print it in a fancy way
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
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
Pickled??
It's a python persistence thingy, don't use it :p
O.o
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
Uhh
?
yes, 2 and 3 say the same thing, I fixed that
they dont do the same thing in the code
No.. Client requests data, server sends
But ok
Menu usually isn't part of it hehe
he wants a menu ;-;
Yeah.
so what you mean a menu isnt part of it?
When you're done I want to see the code 🙂
Must be creative!
so this is the wrong to do this....got it
No
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
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
Listen to the data again with recv
does it send a str by default??
No it sends a byte string
Or rather.. Python gives it to you as a byte string from the recv function
o
So make it an int
Well. Yes. But what's that after the rexv
.decode()
And what does that do
🤔
You need a debugger
Or a lot of prints
(tip put the thing to print in [])
:/
how do I close a socket manually without being able to run the script?
cant start the server
Server is still Running
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
What do you need that for?
What other process is trying to open thensocket then your own server?
huh?
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()
🤔
`print([ch])
it thinks I sent 49 but I sent 1
... 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
one way to do it would be to gather info on the client and sent it in one go to the server
🤦♀️
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??
Menu lives entily in the client. Server just accept parameters and returns the answer
how though? Menu calls functions that are on the server
How do you normally do a http request?
;-; now im too confused
Fuck it, im adding each option to the menu instead of some being in the function
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"))
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
Is that a requirement
? No
Don't bother
Yes
It is
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
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
yeh but how
send it along with the response
huh??
make it part of the array
added color to the Client output :D