FAQ
Hello everyone,

I get a (11, 'Resource temporarily unavailable') error when I try to
send a file using a socket. Is there s size limit? I tried sending a
smaller file and ii poses no problem. Am I doing something wrong? Here
is the code:

def sendMessage(host, port, msg):

if isinstance(msg, unicode):
msg = msg.encode("utf-8")

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
sock.setblocking(0)
totalsent = 0
while totalsent < len(msg):
sent = sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError, "socket connection broken"
totalsent = totalsent + sent
sock.close()

Thank you,
Gabriel

Search Discussions

  • Gabriel Rossetti at Aug 14, 2009 at 7:47 am

    Gabriel Rossetti wrote:
    Hello everyone,

    I get a (11, 'Resource temporarily unavailable') error when I try to
    send a file using a socket. Is there s size limit? I tried sending a
    smaller file and ii poses no problem. Am I doing something wrong? Here
    is the code:

    def sendMessage(host, port, msg):

    if isinstance(msg, unicode):
    msg = msg.encode("utf-8")
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    sock.setblocking(0)
    totalsent = 0
    while totalsent < len(msg):
    sent = sock.send(msg[totalsent:])
    if sent == 0:
    raise RuntimeError, "socket connection broken"
    totalsent = totalsent + sent
    sock.close()

    Thank you,
    Gabriel
    Actually, the original code didn't have the sock.setblocking(0), the
    problem I am trying to find is that the server does have
    sock.setblocking(0) (I can't change that) and it is getting the same
    error as my client has with the sock.setblocking(0), except it gets it
    on the accept() call. I tried modifying my code to be like this :

    def sendMessage(host, port, msg):

    if isinstance(msg, unicode):
    msg = msg.encode("utf-8")

    burstSize = 4096

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    while msg:
    sent = sock.send(msg[:burstSize])
    print "Sending %d bytes..." % sent
    if sent == 0:
    raise RuntimeError, "socket connection broken"
    msg = msg[burstSize:]
    sock.close()

    thinking maybe if I send small parts it would work better but this does
    not seem to change anything.
    How are large msgs sent w/ a socket in python to a non-blocking server?
    The msg I'm trying to send is 175213 bytes long.

    Thank,
    Gabriel
  • Hendrik van Rooyen at Aug 14, 2009 at 8:16 am

    On Friday 14 August 2009 09:47:50 Gabriel Rossetti wrote:
    Gabriel Rossetti wrote:
    8< ------------------------------------------
    Actually, the original code didn't have the sock.setblocking(0), the
    problem I am trying to find is that the server does have
    sock.setblocking(0) (I can't change that) and it is getting the same
    error as my client has with the sock.setblocking(0), except it gets it
    on the accept() call. I tried modifying my code to be like this :

    def sendMessage(host, port, msg):

    if isinstance(msg, unicode):
    msg = msg.encode("utf-8")

    burstSize = 4096

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    while msg:
    sent = sock.send(msg[:burstSize])
    print "Sending %d bytes..." % sent
    if sent == 0:
    raise RuntimeError, "socket connection broken"
    msg = msg[burstSize:]
    sock.close()

    thinking maybe if I send small parts it would work better but this does
    not seem to change anything.
    How are large msgs sent w/ a socket in python to a non-blocking server?
    The msg I'm trying to send is 175213 bytes long.
    Google for netstring, and also look at sock.sendall instead of send.

    - Hendrik
  • Hendrik van Rooyen at Aug 14, 2009 at 8:11 am

    On Friday 14 August 2009 09:15:34 Gabriel Rossetti wrote:
    Hello everyone,

    I get a (11, 'Resource temporarily unavailable') error when I try to
    send a file using a socket. Is there s size limit? I tried sending a
    smaller file and ii poses no problem. Am I doing something wrong? Here
    is the code:

    def sendMessage(host, port, msg):

    if isinstance(msg, unicode):
    msg = msg.encode("utf-8")

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    sock.setblocking(0)
    This is the problem - if the socket does not block, it will return
    the " temporarily unavailable" error when it is busy.

    If you want to use it non blocking then you have to handle the
    error in a try except, and loop.

    - Hendrik
  • Grant Edwards at Aug 14, 2009 at 2:12 pm

    On 2009-08-14, Gabriel Rossetti wrote:

    I get a (11, 'Resource temporarily unavailable') error when I
    try to send a file using a socket. Is there s size limit?
    No, there's no size limit. However, there is a bandwidth
    limit. You can't shove bytes into the pipe faster than they
    come out the other end (at least not over the long term).
    I tried sending a smaller file and ii poses no problem. Am I
    doing something wrong?
    Yes. If you want to have the socket in non-blocking mode, then
    you have to catch EAGAIN and retry the operation.
    def sendMessage(host, port, msg):

    if isinstance(msg, unicode):
    msg = msg.encode("utf-8")

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    sock.setblocking(0)
    totalsent = 0
    while totalsent < len(msg):
    sent = sock.send(msg[totalsent:])
    if sent == 0:
    raise RuntimeError, "socket connection broken"
    totalsent = totalsent + sent
    sock.close()
    --
    Grant Edwards grante Yow! I didn't order any
    at WOO-WOO ... Maybe a YUBBA
    visi.com ... But no WOO-WOO!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 14, '09 at 7:15a
activeAug 14, '09 at 2:12p
posts5
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase