Difference between revisions of "Socket Programming in Python"

From PeformIQ Upgrade
Jump to navigation Jump to search
Line 19: Line 19:
</pre>
</pre>


==Simple Server==
==Simple HTTP Server==


<pre>
<pre>
Line 35: Line 35:


serversocket.listen(5)
serversocket.listen(5)
while True:
  # Accept connections from outside
 
  (clientsocket, address) = serversocket.accept()
  # Now do something with the clientsocket -
  # say, pretend this is a threaded server
  ct = client_thread(clientsocket)
  ct.run()
</pre>
</pre>



Revision as of 16:12, 31 August 2008

References

Examples

Client

#!/usr/bin/env python

# Create an INET, STREAMing socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#now connect to the web server on port 80 - the normal http port

s.connect(("www.performiq.com.au", 80))

Simple HTTP Server

#!/usr/bin/env python

# Create an INET, STREAMing socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to a public host ip, and a well-known port

serversocket.bind((socket.gethostname(), 80))

# Become a server socket

serversocket.listen(5)

while True:
   # Accept connections from outside
   
   (clientsocket, address) = serversocket.accept()

   # Now do something with the clientsocket -
   # say, pretend this is a threaded server

   ct = client_thread(clientsocket)

   ct.run()