PDA

View Full Version : hacked this together for Scrapz...


inkedmn
11-13-2002, 07:40 AM
he posted a question on lno, thought my response might be something the python n00bs could learn from.

this is a really simple server that returns the current date/time whenever anything connects to it, and a client to test it:

the SERVER:

# simple socket communication example
# server listens for connections and returns the current date/time

# SERVER
import socket
import time

host = "localhost" # set host name
port = 1234 # set port number

# create socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind host, port to socket object
sock.bind((host,port))
# set socket object to listen for up to 5 connections
sock.listen(5)
# cute status message
print "waiting for connection"

# socket.accept returns a tuple of the connection, an address
conn, addr = sock.accept()
# another cute status message
print addr, "connected"
# while connection is active
while 1:
# receives up to 1024 bytes of data
data = conn.recv(1024)
# if there's no data, break from the while loop
if not data: break
# send the current date/time back to the client
conn.send(time.ctime())
# close the socket
conn.close()


the CLIENT:

# simple socket communication example
# client connects to server we created

# CLIENT

import socket

host = "localhost" # set host name (same as server)
port = 1234 # set port number

# create socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to host on port (to the server)
sock.connect((host, port))
# send some data
sock.send("what time is it?")
# receive data up to 1024 bytes
data = sock.recv(1024)
# print a nicely formatted response
print "Server response:", data
# close the socket
sock.close()


anyway, just thought somebody might learn from this. i'll move it to tutorials if that's the general consensus (don't know if it'd call this a "tutorial" though... :))

jemfinch
11-13-2002, 01:07 PM
There is a major problem with that server, though. Let's say you run it on port 7 (which, iirc [which I probably don't] is the date port). Try this in your python interpreter (with the server running already as a separate process):


>>> from socket import *
>>> s = socket(AF_INET, SOCK_STREAM)
...
>>> s.connect(('localhost', 7))
...
>>> s1 = socket(AF_INET, SOCK_STREAM)
...
>>> s1.connect(('localhost', 7))
...
>>> s1.recv(1024)
(untested)

That call to send should block, because the original client (the socket s you made and connected with earlier) didn't read what the server sent, and so the server's send should be blocked. There's a possibility that's not the case because of buffering the OS does, but that's not realiable (especially since the OS will only buffer a certain amount of data, and thus in a larger application you'd be almost sure to hit that limit).

So you can't write a serious server that way -- you'll have to use asyncore or asynchat or Twisted or threads or some other asynchronous framework.

Jeremy

inkedmn
11-13-2002, 02:16 PM
Originally posted by jemfinch
...you can't write a serious server that way -- you'll have to use asyncore or asynchat or Twisted or threads or some other asynchronous framework.


writing a serious server was never my intention. somebody just wanted an example of making python programs talk over sockets, and that's what this is; nothing more. if i were actually writing a server that was going to be used by me or a business, i would've taken much more care in it's design. but, since this was just a crappy example of the mechanics of it (and i wrote the code in about 5 minutes), i'm ok with it sucking and being bad coding practice as long as the principles are conveyed.

[edit]
just re-read this post and i thought i'd add that i'm not upset, and if my post gives that impression, ignore it :)

jemfinch
11-13-2002, 03:40 PM
It's a great and clear example of how to use sockets in Python.

Just make sure the person you're writing it for knows that it's not an adequate solution, that there's more to learn about writing good servers :)

I wasn't trying to insult you or your code at all, just trying to head off another question before it happened :)

Jeremy

GnuVince
11-13-2002, 04:14 PM
Friends, do y'all remember when good ol' inkedmn was asking us how to do an "if"?

:'(

Scrapz
11-16-2002, 07:20 AM
:D

inkedmn
11-16-2002, 09:09 PM
Scrapz!!!

welcome aboard, friend :)