Author Topic: Using Python, how do I send data from client to server?  (Read 682 times)

I'm studying networking in Python, i'm highly beginner at networking, but I do have experience in general python. How do I send data from client to the server? Here is some code that I found:

Server
Code: [Select]
#!/usr/bin/python           # This is server.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12346                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port
s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send('Thank you for connecting')
   c.close()                # Close the connection


Client
Code: [Select]
#!/usr/bin/python           # This is client.py file

import socket               # Import socket module

s = socket.socket()
# Create a socket object
host = socket.gethostname() # Get local machine name
port = 12346                # Reserve a port for your service.

s.connect((host, port))
print s.recv(1024)
s.close                     # Close the socket when done

I just want the client to send a message to the server, and for it print in the server console. Please help.

ht tp://www.youtube.com/watch?v=t348e24vDyA&lc=z6kypGcF3BLOIDNHwyNrnYiYHqnXVjjRgYxSObxRVpU

User was banned for this post
« Last Edit: February 11, 2013, 06:12:56 PM by Badspot »


http://www.youtube.com/watch?v=t348e24vDyA&lc=z6kypGcF3BLOIDNHwyNrnYiYHqnXVjjRgYxSObxRVpU

Good post bro.


Networking seems like it's super complicated to normal programming. Even like managing a network with software seems pretty complicated.


Networking seems like it's super complicated to normal programming. Even like managing a network with software seems pretty complicated.
Tell me about it. Wikipedia dictionary results aren't helping either.

Tell me about it. Wikipedia dictionary results aren't helping either.

You look up any type of networking in python tutorials?

You look up any type of networking in python tutorials?
I found a youtube tutorial 2 weeks ago explaining how to make a server send messages to a client, but then once I inputted everything, Python gave it an error then I looked at the date and it said it was made like a long time ago, where the code is outdated.

Today I found a tutorial which was more like a reference rather than a tutorial. Nonetheless, I still got the code you see in the OP from it, and it works perfectly. I just start the server then start the client and the server prints that the client connected to the server. I could probably modify this but I never tampered with sockets or addresses so i'm not really sure how to make the client send data to the server.

Really all I want to make here is a server-sided save data system, where the client plays a game, and it sends data to the localhost server every time he does something, and when he quits and re-logs back in, it loads his stuff.

For ease of debugging it might be easier to first make sure that the server works, before you start on the client. Other than that:

You forgot () when closing in the client.

Use '127.0.0.1' instead of binding binding/connecting to 'socket.gethostname()'. If you want others to be able to connect, bind on '0.0.0.0'.

Also, you're using the low-level socket library, which might be a bad idea for an introduction. Sadly, while Python has a standard higher-level socket server library (SocketServer), there isn't anything like that included for clients. Instead, I'd recommend that you look at Twisted (for both of them), which is an event-based high-level networking library, which can automatically handle things like waiting for EOLs, or automatically reconnecting, for you.

You look up any type of networking in python tutorials?
The problem with networking in Python (especially clients) is that there isn't a nice general way to do it. You either have socket which is pretty low-level, or you have Twisted, which is like a whole ecosystem on it's own. There are also some other approaches like gevent (which is a monkey-patch around low-level libraries so that you can use socket-based libraries to do somewhat Twisted-resembling stuff) or Diesel (which is just a piece of crap at the moment).

I found a youtube tutorial 2 weeks ago explaining how to make a server send messages to a client, but then once I inputted everything, Python gave it an error then I looked at the date and it said it was made like a long time ago, where the code is outdated.

Today I found a tutorial which was more like a reference rather than a tutorial. Nonetheless, I still got the code you see in the OP from it, and it works perfectly. I just start the server then start the client and the server prints that the client connected to the server. I could probably modify this but I never tampered with sockets or addresses so i'm not really sure how to make the client send data to the server.

Really all I want to make here is a server-sided save data system, where the client plays a game, and it sends data to the localhost server every time he does something, and when he quits and re-logs back in, it loads his stuff.
What version of Python do you have installed?