Showing posts with label terminal. Show all posts
Showing posts with label terminal. Show all posts

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. 

Thursday, 15 September 2011

Dictionaries

Creating a dictionary in Python


It's easier than you (I) would think!
And it has humoured me this morning. Really tickled my script-buds.


I created a dictionary called 'student'. Within the dictionary we assigned different values (see below for mine). You can then make queries within the dictionary to find if something exists in there or not.


I asked if we had any creative license with this, we were granted a small amount of creative license and I rolled with it like a stone gathering a shed-load of moss.


student = {'alcohol': '60%','facility_to_learn': '15%', 'tolerance': '5%', 'culinary_skills': '4%', 'project_management': '7%', 'arithmatic_skills': '10%'}


You can add extra values later by using this:


student['enthusiasm'] = '6.5%'


To query the dictionary you need to type something like:


'alcohol' in student


And it returns a Boolean (Bool) value (True or False).



>>> 'alcohol' in student
True


After multiple attempts I had created this.





Although this was humorous I then learnt that '4%' is not a numeric value, but a string. A string is literally a group of characters, not a number (or a percentage as I wanted). This meant you could not query whether this student had an alcohol rating of above 50% (student['alcohol']  > '50%').


Instead I had to rewrite my dictionary to get rid of my '%'s, which rendered my 'arithmatic_skills': '10%' not funny (add up original values as %'s to see the funny side). Bad Time Bear had come out to play :/ 
Having said that I managed to complete the task at hand.







However this can be hugely helpful when searching through databases and returning values of unknown quantities ie. you can return all the ' 'student IDs' > 000100 ' or all students whose ' course == Digital_Journalism ' etc.





I can't help but feel like I've created the geekiest form of top trumps ever but you can then add more students to your dictionary.
Firstly, you create new students with the same items and values (if each has an enthusiasm value it's easier to search).













Then you create a name for your database, in this case 'students' and include all the names of your dictionaries.


You can [import, then] use pprint to display the list of items in a much easier to read manner (pprint.pprint(lab)).










I also learnt that 6 > a . Profound.

Monday, 12 September 2011

1st week

The words "Don't panic", closely followed by "I can see you're all panicking" cut through the silence on Monday morning. Zimmer was trying to calm the nerves.


I had no real idea what to expect from the next three weeks.


We gathered in the lecture theatre of the New Academic Building at Goldsmiths College for a quick introduction to our Digital Methods Bootcamp.
We were introduced to the computer labs and to both Python and Javascript within Emacs (luckily using an iMac rather than an eMac).


Between the hours of 10am and 5pm my mind melted beyond recognition, a bit like those candles you get at Cargo and the like, that smell of generic petals which ends up looking like the blood splatter on the face of the smiley from the Watchmen.


Yes, my mind was more jumbled and tenuous than that last comic book reference.


Day 2 morning was a little less mind boggling, and I even managed to work out how to adapt some Python script beyond what was on the handout which felt like a massive accomplishment after Day 1.
The afternoon session made my cerebral cortex misfire.


Day 3 was all about the written word. We were looking at manipulating and managing directories and files through mostly Terminal. Pipes (|)and grep were difficult to get to grips with, but I soon became a dab-hand at it (!). My favourite take-aways on the third day was the discovery of the built-in device files; "/dev" .
By using a move command in terminal, you can purge any folder/file by mv ~ /dev/null . There is also /dev/random which serves as a random number generator and /dev/full which always returns a 'disk full' message.


Our fourth and final day of the first week saw us work on some python scripts, writing in Emacs and executing them in terminal. It was great to start using some real world examples of the work we'd been looking at over the last few days. We used a few rss links, BBC News and Twitter searches to play with.
We also had Director Brauer of CAST come in and enthuse about the course, and more importantly us. It made us feel good to be getting started, and that perseverance was crucial at this stage. We heard rumours of the WSJ and Guardian speakers as well as real-time collaborations with Columbia University, a BBQ in our seminar room and sit-ups with our peers and lecturers in our booked time in the gym!


The week drew to a close we ended with some much needed drinks and musings on the last few days.


I then fell over a bell on the side of the road and into a traffic light.


(not my image)


Now I'm bedded in at the new flat and working hard in week two with html, xml and markup languages to look forward to.