|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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! |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
Can you maybe provide a server example as well ?
Thanks for the sample code as well, it's much appreciated. |
|
#4
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Sockets ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|