Results 1 to 3 of 3

Thread: Sockets in python

  1. #1
    Join Date
    Aug 2008

    Sockets in python

    Hey there guys, been looking into sockets in python recently but i'm having a little trouble getting my program to work rite.

    It's just a random number generator game but I want to make it available over a network. So people can connect, play and then quit. I know how to open a socket then close it. I've done the basic examples in some tutorials but what I don't get(and i'm sure it's something stupid that i'm just not realising) is how to actually incorporate it into my program.

    print "Hello, World!"
    #I put "Hello World" into all of my programs.  Just as a kind of good practise thing.
    # Guessing Game
    import random
    # Start while loop
    number = random.randrange(1,100)
    guess = 0
    while guess != number:
       guess = int(raw_input("What is your guess? "))
       if guess == number:
          print "Well done you got it!"
       elif guess < number:
          print "Sorry it's higher than that"
       elif guess > number:
          print "Sorry it's lower than that"
    print "Thanks for playing random!"
    That's the game. I'm sure it's pretty ugly code so if anyones got any suggestions on how to clean it up that would also be cool.

  2. #2
    Join Date
    Aug 2008
    Here is the socket example I got from a tutorial
    print "Hello, World!"
    # A simple echo server
    A simple echo server
    import socket 
    host = ''
    port = 50000
    backlog = 5
    size = 1024
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    while 1:
       client, address = s.accept()
       data = client.recv(size)
       if data:

  3. #3
    Join Date
    May 2008
    Heh, I'm not a Python guy, but sockets are usually a general concept based around the Berkeley C implementation. In that essecene you generally would write to the socket as if though it were a file. See in C a socket descriptor is pretty much the same as a file descriptor except that you can only move through it sequentially and can't seek back and forth like you can through a file. Not sure how python handles files though so this could be totally different. **** maybe py treats a socket as a descriptor or maybe its a class. if it is a descriptor you should be able to write to it just like a file. If it is a class then see if socket has methods like write or send and read or recv. I'll try and get a glimpse at pythons socket implementation and point you better when I'm sober and such.

Similar Threads

  1. AM2+ vs AM3 sockets
    By Xymaya in forum Hardware Peripherals
    Replies: 6
    Last Post: 21-11-2010, 05:39 AM
  2. Problem with Sockets and Protocols
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 06:23 AM
  3. Binary sockets in C#
    By CheckMeNot in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 11:02 AM
  4. Download Python 3.0 / Python 3000
    By Amaresh in forum Software Development
    Replies: 6
    Last Post: 24-02-2009, 09:28 AM
  5. Using sockets correctly in unix
    By Rodney123 in forum Software Development
    Replies: 2
    Last Post: 05-11-2008, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts