Thread: Socket

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    california
    Posts
    53
    Rep Power
    10

    Socket


    I have been trying to figure out the whole socket module for the past couple of days. I need to know what to plug in for this>>>

    import socket
    socket.getsockname(...)

    I want it to return the address info.. so what do i plug in the ()
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Hi,

    socket is just the module name getsockname() is not a function in the socket module it is a method of a socket object.

    You need to create a socket object first then you can use that objects getsockname() method.

    Socket Programming Howto
    python Code:
    >>> import socket
    >>> mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    >>> mysocket.connect(("www.bbc.co.uk",80))
    >>> mysocket.getsockname()
    ('10.0.0.79', 1192)


    grim
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    There's also a nice article about socket programming here on Devshed that you might enjoy. I didn't write this one *thinks something arrogant* .

    http://www.devshed.com/c/a/Python/Sockets-in-Python/

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Lisbon, Portugal
    Posts
    154
    Rep Power
    12
    Sockets are horribly hard to master. You get away with simple single-connection stuff with whatever you find online, but if you're going for more robust stuff, you're pretty much out of luck. I've posted stuff about sockets here a couple of times, but it becomes too complicated too easilly, and testing is usually a big mess, so the people who truly know about it don't bother.

    That's the reason I am waiting for my copy of Foundations of Python Network Programming which I hope will do me some good and won't just explain lots of modules I don't need right now. I'll try posting a review once I'm done with it.
    "Get it hot! Hit it harder!!!"
    --- The tutor warcry

    ActivePython
    Mark Pilgrim's Dive into Python E-book, thank this guy for his contribution to the Python community!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    california
    Posts
    53
    Rep Power
    10
    oh ok its starting to make alot more sence. thank you for all the information. I would be lost with out you guys lol
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    california
    Posts
    53
    Rep Power
    10
    oh one more thing. were did you get the 80? after the url. why is that there?
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by miryln
    oh one more thing. were did you get the 80? after the url. why is that there?
    It's the port where that server can be accessed on. You can think of it as a sort of cubbyhole and you're telling the program which one it can find the server in. The web-server as a rule functions on port 80 .

    You may wish to read a little about networking if you're not sure what a port is or you'll likely have problems learning to do socket programming.

    Night all ,

    Mark.

    Comments on this post

    • xlordt agrees : have some rep :)
    programming language development: www.netytan.com Hula

  14. #8
  15. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    this and this should keep you busy

    Ports
    Ports are aoftware communications channels represented by numeric values. Different networking protocols operate on different port numbers.

    Comments on this post

    • netytan agrees : Theres my man ;). Outa rep sorry.
    • miryln agrees : nice this helps thanks
  16. #9
  17. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    In my experience sockets are easier to write for on Linux. You can operate in non blocking mode much easier than Windows.

    grim
    Last edited by Grim Archon; February 22nd, 2006 at 02:12 AM.
  18. #10
  19. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by Grim Archon
    In my experience sockets are easier to write for on Linux. You can operate in non blocking mode much easier than Windows.

    grim
    sockets aren't hard to master... low level sockets that is... now.. try mastering layers

    Comments on this post

    • miryln agrees : lets hope soo haha
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Lisbon, Portugal
    Posts
    154
    Rep Power
    12
    xlordx, if it's not an indiscretion, could you tell me what you used to master them? I definitely need some sources. I'm hoping the book'll help, but I could do with knowing as much as possible.

    Thanks in advance!
    "Get it hot! Hit it harder!!!"
    --- The tutor warcry

    ActivePython
    Mark Pilgrim's Dive into Python E-book, thank this guy for his contribution to the Python community!

IMN logo majestic logo threadwatch logo seochat tools logo