|
|
A simple client/server application in Python # Python Aug 09 2003 - 06:55 EST Posted By numberXIII
(0) | One of the most interesting feature in a programming language (in my point of view) is the ability to program sockets, that establishes a direct connection on various ports of a computer either waiting for a connection ( server model) or sending information to an already open port ( client model). The language here will be Python.
It is an nice high level programming language with loose typing , that is variables are declared when initialized and you don’t have to worry about what type they are , python will set the type for you! Now to our sockets. For this example we are going to establish a server socket and bind it to an open port. It will wait for a connection and print what is sent to it. The client will send information to the server socket ( in this example , the current time) and the server will print the time it receives from the client.
Server socket:
# Server socket:
import socket
import time
s = socket(AF_INET, SOCK_STREAM) # initialize TCP socket
s.bind((“”,8888)) # bind the socket to port 8888
s.listen(5) # the socket starts listening on port 8888 with a maximum of 5 connections
while 1:
client, addr = s.accept() # wait for a connection
print “ Got a connection from “, addr
client.send (time,ctime(time.time())) # send time back
client.close() # close the connection
Client socket:
# Client socket:
import socket
s = socket(AF_INET, SOCK_STREAM) # initialize TCP socket such as the server socket
s.connect((“localhost”,8888)) # connect to server socket
tm = s.recv(1024) # receive a maximum of 1024 bytes
s.close() # close socket
print “ The current time is”, tm
For more information on python, here are some links:
http://www.python.org
The official python website
http://www.awaretek.com/tutorials.html
Great list of python tutorials
http://www.scriptol.org/python.html
nice introduction to the language
XIII
numberXIII@Phreaker.net
(0)
|
|
| To the best of our knowledge, the text on this page either may be freely reproduced and distributed or was written specifically for fromadia.com. The site layout, page layout, and all original artwork on this site are Copyright © 2002 Fromadia.com. If you wish to reproduce any of it or if you are author of a work that you feel shouldn't be printed here, please email us at copyright@fromadia.com. | |
|
|