June 20th, 2004, 09:40 PM
-
Sockets ?
I've read online both in ebooks and tutorials on how sockets work. All of which give a different example on how to get this set up. I've searched this forum as well, but can anyone provide an example of code that just sets up a socket and the client sends text and then ends the connects with the server socket.
Thanks!
June 20th, 2004, 10:39 PM
-
Quick example (no error checking):
Code:
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((127.0.0.1, 50007))
mysock.send("Hello, World!")
mysock.close()
Look here for more info: http://docs.python.org/lib/module-socket.html
June 20th, 2004, 11:13 PM
-
Can you maybe provide a server example as well ?
Thanks for the sample code as well, it's much appreciated.
June 21st, 2004, 09:01 AM
-
Code:
import
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 50007))
server.listen(1024)
(client, addr) = server.accept()
print "Connection: ", addr[0]
input = client.recv(1024)
print "Received: ", input
client.close()
server.close()
It shouldn't be too hard to expand on these to make them interactive if you so desire.
EDIT: There are some good protocol dependent and indepentent examples here: http://docs.python.org/lib/socket-example.html