Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

jobs.webdeveloper.com

e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Breaking Down a Simple Python App

Now for the client side:

   1 #!/usr/bin/env python
   2 # a simple TCP client
  
   3 from socket import *
   4 from time import time
   5 from time import sleep
   6 import sys
   7 BUFSIZE = 4096
  
   8 class CmdLine:
   9     def __init__(s,host):
  10         s.__HOST = host
  11         s.__PORT = 29876
  12         s.__ADDR = (s.__HOST,s.__PORT)
  13         s.__sock = None
  
  14     def makeConnection(s):
  15         s.__sock = socket( AF_INET,SOCK_STREAM)
  16         s.__sock.connect(s.__ADDR)
  
  17     def sendCmd(s, cmd):
  18         s.__sock.send(cmd)
  
  19     def getResults(s):
  20         data = s.__sock.recv(BUFSIZE)
  21         print data
   
  22 if __name__ == '__main__':
  23     conn = CmdLine('localhost')
  24     conn.makeConnection()
  25     conn.sendCmd('DUMMY CMD')
  26     conn.getResults()
  27     conn.sendCmd('BYE')

Line 15 creates a TCP socket, just like the server program. Line 16 creates a connection to the server. Remember, the server is "listening" for this connection. You must provide in HOST the IP address of the server or a resolvable host name. In this example we use "localhost" which will normally resolve to the loopback address. In line 18 we send a message to the server. In line 20 we receive a message from the server. That is it. That is all the communication necessary.

Of course this is a very simple example, which does not accomplish anything useful. But with some modification, these two classes can be used for your client server application. In a simple environment, the TCP stack would buffer enough packets so that the server could keep up with its job of listening.

In a more complicated environment, multiple threads might be required and possibly queues, however these are subjects for another article.

Breaking Down a Simple Python App
Breaking Down a Simple Python App


Up to => Home / Authoring / Tutorials / Python




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers