Monday 19 September 2011

Creating your own server


Today we've looked at creating our own servers.


We began by pinging the servers of some household names (google, facebook, twitter - the slowest, who'da thunk?!), then following the route of a packet between here and the US servers, and then having a look at the new IPv6 addresses including facebook's '2620:0:1cfe:face:b00c::3'.


Using Python once again, we created a script which is akin to Tim Henman's famous serve *run to the net* and volley approach.


By running our two scripts each in its own terminal window we could try and connect to a newly created socket, printing on the server side the sent & received data, and on the client facing side, printing a personalised message (in red here).


Here is "Tiger Tim's" serve[r].


Socket script


import socket


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


s.bind(('127.0.0.1', 4900))
s.listen(1)


client_socket, address = s.accept()
print 'received connection from ', address


while True:
   data = client_socket.recv(4096)
   if not data:
      client_socket.close()
      break


   print 'received data', data


   bytes_sent = client_socket.send(data)
   print 'sent:', bytes_sent


s.close


And here is his volley (in a very abstract and perhaps incorrect analogy).


Connection script



import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
s.connect(('127.0.0.1', 4900))
s.send('Bugger off!')
print s.recv(4096)
s.send('Oh, okay. Come on in...')
print s.recv(4096)
s.close()

In a typically British Semi-Final kind of way, my first attempt crashed out, causing an outcry of (albeit, excpeted) anguish and demoralising anti-nationalism.


Why I hear you cry?


Because I spelt 'True' with a non-capitalised 't'.


Once again, Python had spanked me with its racket of discipline and grammar.


*spank* An artists impression of Python giving me a once over using my Henman metaphor.


I will learn soon enough to avoid the wrath of Python. And people shall flock to a hill named after my coding...

...only to leave with a bitter resentment of losing days to rained-off exasperation, filled with the false hopes of being part of a moment in history.

At least will be strawberries and cream.

Cheering on Java vs Snake.

I also managed to sort out my public folder on the University server.

It can be accessed here

No comments:

Post a Comment