Python sendto telling me it needs an int












2















from socket import *
import packets
image = "testfile.bmp"

# open image
bufferSize = 2048
myfile = open(image, 'rb')
sequenceNumber = 0
totalBytes = 0

serverName = "127.0.0.1"
serverSendPort = 12000
serverSendPort = int(serverSendPort)
serverListenPort = 12001
clientSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket = socket(AF_INET, SOCK_DGRAM)

while (1):
if (sequenceNumber == 0):
data = packets.mkepckt(myfile.read(bufferSize), 0,
packets.calculateChecksum(myfile.read(bufferSize), bufferSize))
clientSocket.sendto(bytearray(data),(serverName, serverSendPort))
sequenceNumber = 1

ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

while (ACK[0] is not 0 or ACKchecksum is not ACK[2]):
clientSocket.sendto(data, (serverName, serverSendPort))
ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

elif (sequenceNumber == 1):
data = packets.mkepckt(myfile.read(bufferSize), 1,
packets.calculateChecksum(myfile.read(bufferSize), bufferSize))
clientSocket.sendto(bytearray(data),(serverName, serverSendPort))
sequenceNumber = 0

ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.checksum(bufferSize, ACK[1])

while (ACK[0] is not 1 or ACKchecksum is not ACK[2]):
clientSocket.sendto(data, (serverName, serverSendPort))
ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

myfile.close()

clientSocket.close()
serverSocket.close()


Not sure I needed to put my whole code, but better safe than sorry.
So for some reason, despite the fact that I have an int in the sendto. I have also tried all sorts of typecasting, from typecasting each individual element to typecasting the whole thing as an int, and I still get this error.










share|improve this question


















  • 1





    What is the EXACT error message? Your calls to sendto() are inconsistent, though. Some of them are passing data directly, while others are passing bytearray(data) instead.

    – Remy Lebeau
    Nov 14 '18 at 20:59













  • The first error I got was that "TypeError("a bytes-like object is required, not 'tuple'")", so I changed I typecasted the first one, but I must have forgotten to typecast the rest. As for the exact error: "TypeError('an integer is required')"

    – DPalmz
    Nov 15 '18 at 17:14











  • I'm no Python expert (I'm not even a Python user), but your calls to sendto() look OK to me. What kind of object is mkepckt() returning exactly? Are you sure it is a byte array? Also, your calls to calculateChecksum() when calling mkepckt() look suspiciously wrong to me. You are calling myfile.read() twice, so you are passing different bytes to mkepckt() and calculateChecksum(). Shouldn't you be passing the same bytes to both? Something like: data = myfile.read(bufferSize) data = packets.mkepckt(data, 0, packets.calculateChecksum(data, len(data)))

    – Remy Lebeau
    Nov 15 '18 at 17:35













  • Also, when calling calculateChecksum() after recvfrom(), shouldn't you be using len(ACK) instead of BufferSize? Passing BufferSize to recvfrom() is the maximum number of bytes to read, but that is no guarantee that is the number of bytes actually read, it may be less.

    – Remy Lebeau
    Nov 15 '18 at 17:39













  • Make packet is a tuple. It really just takes all those objects and adds them together into a local variable then returns it. I mostly did it this way so I can have all my "packet" functions in one file, to keep things clean. As for those other two things, I believe you are absolutely correct. I have changed them, thanks. Still getting that int error though, not sure what to do about it.

    – DPalmz
    Nov 16 '18 at 3:58
















2















from socket import *
import packets
image = "testfile.bmp"

# open image
bufferSize = 2048
myfile = open(image, 'rb')
sequenceNumber = 0
totalBytes = 0

serverName = "127.0.0.1"
serverSendPort = 12000
serverSendPort = int(serverSendPort)
serverListenPort = 12001
clientSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket = socket(AF_INET, SOCK_DGRAM)

while (1):
if (sequenceNumber == 0):
data = packets.mkepckt(myfile.read(bufferSize), 0,
packets.calculateChecksum(myfile.read(bufferSize), bufferSize))
clientSocket.sendto(bytearray(data),(serverName, serverSendPort))
sequenceNumber = 1

ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

while (ACK[0] is not 0 or ACKchecksum is not ACK[2]):
clientSocket.sendto(data, (serverName, serverSendPort))
ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

elif (sequenceNumber == 1):
data = packets.mkepckt(myfile.read(bufferSize), 1,
packets.calculateChecksum(myfile.read(bufferSize), bufferSize))
clientSocket.sendto(bytearray(data),(serverName, serverSendPort))
sequenceNumber = 0

ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.checksum(bufferSize, ACK[1])

while (ACK[0] is not 1 or ACKchecksum is not ACK[2]):
clientSocket.sendto(data, (serverName, serverSendPort))
ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

myfile.close()

clientSocket.close()
serverSocket.close()


Not sure I needed to put my whole code, but better safe than sorry.
So for some reason, despite the fact that I have an int in the sendto. I have also tried all sorts of typecasting, from typecasting each individual element to typecasting the whole thing as an int, and I still get this error.










share|improve this question


















  • 1





    What is the EXACT error message? Your calls to sendto() are inconsistent, though. Some of them are passing data directly, while others are passing bytearray(data) instead.

    – Remy Lebeau
    Nov 14 '18 at 20:59













  • The first error I got was that "TypeError("a bytes-like object is required, not 'tuple'")", so I changed I typecasted the first one, but I must have forgotten to typecast the rest. As for the exact error: "TypeError('an integer is required')"

    – DPalmz
    Nov 15 '18 at 17:14











  • I'm no Python expert (I'm not even a Python user), but your calls to sendto() look OK to me. What kind of object is mkepckt() returning exactly? Are you sure it is a byte array? Also, your calls to calculateChecksum() when calling mkepckt() look suspiciously wrong to me. You are calling myfile.read() twice, so you are passing different bytes to mkepckt() and calculateChecksum(). Shouldn't you be passing the same bytes to both? Something like: data = myfile.read(bufferSize) data = packets.mkepckt(data, 0, packets.calculateChecksum(data, len(data)))

    – Remy Lebeau
    Nov 15 '18 at 17:35













  • Also, when calling calculateChecksum() after recvfrom(), shouldn't you be using len(ACK) instead of BufferSize? Passing BufferSize to recvfrom() is the maximum number of bytes to read, but that is no guarantee that is the number of bytes actually read, it may be less.

    – Remy Lebeau
    Nov 15 '18 at 17:39













  • Make packet is a tuple. It really just takes all those objects and adds them together into a local variable then returns it. I mostly did it this way so I can have all my "packet" functions in one file, to keep things clean. As for those other two things, I believe you are absolutely correct. I have changed them, thanks. Still getting that int error though, not sure what to do about it.

    – DPalmz
    Nov 16 '18 at 3:58














2












2








2








from socket import *
import packets
image = "testfile.bmp"

# open image
bufferSize = 2048
myfile = open(image, 'rb')
sequenceNumber = 0
totalBytes = 0

serverName = "127.0.0.1"
serverSendPort = 12000
serverSendPort = int(serverSendPort)
serverListenPort = 12001
clientSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket = socket(AF_INET, SOCK_DGRAM)

while (1):
if (sequenceNumber == 0):
data = packets.mkepckt(myfile.read(bufferSize), 0,
packets.calculateChecksum(myfile.read(bufferSize), bufferSize))
clientSocket.sendto(bytearray(data),(serverName, serverSendPort))
sequenceNumber = 1

ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

while (ACK[0] is not 0 or ACKchecksum is not ACK[2]):
clientSocket.sendto(data, (serverName, serverSendPort))
ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

elif (sequenceNumber == 1):
data = packets.mkepckt(myfile.read(bufferSize), 1,
packets.calculateChecksum(myfile.read(bufferSize), bufferSize))
clientSocket.sendto(bytearray(data),(serverName, serverSendPort))
sequenceNumber = 0

ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.checksum(bufferSize, ACK[1])

while (ACK[0] is not 1 or ACKchecksum is not ACK[2]):
clientSocket.sendto(data, (serverName, serverSendPort))
ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

myfile.close()

clientSocket.close()
serverSocket.close()


Not sure I needed to put my whole code, but better safe than sorry.
So for some reason, despite the fact that I have an int in the sendto. I have also tried all sorts of typecasting, from typecasting each individual element to typecasting the whole thing as an int, and I still get this error.










share|improve this question














from socket import *
import packets
image = "testfile.bmp"

# open image
bufferSize = 2048
myfile = open(image, 'rb')
sequenceNumber = 0
totalBytes = 0

serverName = "127.0.0.1"
serverSendPort = 12000
serverSendPort = int(serverSendPort)
serverListenPort = 12001
clientSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket = socket(AF_INET, SOCK_DGRAM)

while (1):
if (sequenceNumber == 0):
data = packets.mkepckt(myfile.read(bufferSize), 0,
packets.calculateChecksum(myfile.read(bufferSize), bufferSize))
clientSocket.sendto(bytearray(data),(serverName, serverSendPort))
sequenceNumber = 1

ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

while (ACK[0] is not 0 or ACKchecksum is not ACK[2]):
clientSocket.sendto(data, (serverName, serverSendPort))
ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

elif (sequenceNumber == 1):
data = packets.mkepckt(myfile.read(bufferSize), 1,
packets.calculateChecksum(myfile.read(bufferSize), bufferSize))
clientSocket.sendto(bytearray(data),(serverName, serverSendPort))
sequenceNumber = 0

ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.checksum(bufferSize, ACK[1])

while (ACK[0] is not 1 or ACKchecksum is not ACK[2]):
clientSocket.sendto(data, (serverName, serverSendPort))
ACK, = serverSocket.recvfrom(bufferSize)
ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

myfile.close()

clientSocket.close()
serverSocket.close()


Not sure I needed to put my whole code, but better safe than sorry.
So for some reason, despite the fact that I have an int in the sendto. I have also tried all sorts of typecasting, from typecasting each individual element to typecasting the whole thing as an int, and I still get this error.







python-3.x sockets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 20:51









DPalmzDPalmz

112




112








  • 1





    What is the EXACT error message? Your calls to sendto() are inconsistent, though. Some of them are passing data directly, while others are passing bytearray(data) instead.

    – Remy Lebeau
    Nov 14 '18 at 20:59













  • The first error I got was that "TypeError("a bytes-like object is required, not 'tuple'")", so I changed I typecasted the first one, but I must have forgotten to typecast the rest. As for the exact error: "TypeError('an integer is required')"

    – DPalmz
    Nov 15 '18 at 17:14











  • I'm no Python expert (I'm not even a Python user), but your calls to sendto() look OK to me. What kind of object is mkepckt() returning exactly? Are you sure it is a byte array? Also, your calls to calculateChecksum() when calling mkepckt() look suspiciously wrong to me. You are calling myfile.read() twice, so you are passing different bytes to mkepckt() and calculateChecksum(). Shouldn't you be passing the same bytes to both? Something like: data = myfile.read(bufferSize) data = packets.mkepckt(data, 0, packets.calculateChecksum(data, len(data)))

    – Remy Lebeau
    Nov 15 '18 at 17:35













  • Also, when calling calculateChecksum() after recvfrom(), shouldn't you be using len(ACK) instead of BufferSize? Passing BufferSize to recvfrom() is the maximum number of bytes to read, but that is no guarantee that is the number of bytes actually read, it may be less.

    – Remy Lebeau
    Nov 15 '18 at 17:39













  • Make packet is a tuple. It really just takes all those objects and adds them together into a local variable then returns it. I mostly did it this way so I can have all my "packet" functions in one file, to keep things clean. As for those other two things, I believe you are absolutely correct. I have changed them, thanks. Still getting that int error though, not sure what to do about it.

    – DPalmz
    Nov 16 '18 at 3:58














  • 1





    What is the EXACT error message? Your calls to sendto() are inconsistent, though. Some of them are passing data directly, while others are passing bytearray(data) instead.

    – Remy Lebeau
    Nov 14 '18 at 20:59













  • The first error I got was that "TypeError("a bytes-like object is required, not 'tuple'")", so I changed I typecasted the first one, but I must have forgotten to typecast the rest. As for the exact error: "TypeError('an integer is required')"

    – DPalmz
    Nov 15 '18 at 17:14











  • I'm no Python expert (I'm not even a Python user), but your calls to sendto() look OK to me. What kind of object is mkepckt() returning exactly? Are you sure it is a byte array? Also, your calls to calculateChecksum() when calling mkepckt() look suspiciously wrong to me. You are calling myfile.read() twice, so you are passing different bytes to mkepckt() and calculateChecksum(). Shouldn't you be passing the same bytes to both? Something like: data = myfile.read(bufferSize) data = packets.mkepckt(data, 0, packets.calculateChecksum(data, len(data)))

    – Remy Lebeau
    Nov 15 '18 at 17:35













  • Also, when calling calculateChecksum() after recvfrom(), shouldn't you be using len(ACK) instead of BufferSize? Passing BufferSize to recvfrom() is the maximum number of bytes to read, but that is no guarantee that is the number of bytes actually read, it may be less.

    – Remy Lebeau
    Nov 15 '18 at 17:39













  • Make packet is a tuple. It really just takes all those objects and adds them together into a local variable then returns it. I mostly did it this way so I can have all my "packet" functions in one file, to keep things clean. As for those other two things, I believe you are absolutely correct. I have changed them, thanks. Still getting that int error though, not sure what to do about it.

    – DPalmz
    Nov 16 '18 at 3:58








1




1





What is the EXACT error message? Your calls to sendto() are inconsistent, though. Some of them are passing data directly, while others are passing bytearray(data) instead.

– Remy Lebeau
Nov 14 '18 at 20:59







What is the EXACT error message? Your calls to sendto() are inconsistent, though. Some of them are passing data directly, while others are passing bytearray(data) instead.

– Remy Lebeau
Nov 14 '18 at 20:59















The first error I got was that "TypeError("a bytes-like object is required, not 'tuple'")", so I changed I typecasted the first one, but I must have forgotten to typecast the rest. As for the exact error: "TypeError('an integer is required')"

– DPalmz
Nov 15 '18 at 17:14





The first error I got was that "TypeError("a bytes-like object is required, not 'tuple'")", so I changed I typecasted the first one, but I must have forgotten to typecast the rest. As for the exact error: "TypeError('an integer is required')"

– DPalmz
Nov 15 '18 at 17:14













I'm no Python expert (I'm not even a Python user), but your calls to sendto() look OK to me. What kind of object is mkepckt() returning exactly? Are you sure it is a byte array? Also, your calls to calculateChecksum() when calling mkepckt() look suspiciously wrong to me. You are calling myfile.read() twice, so you are passing different bytes to mkepckt() and calculateChecksum(). Shouldn't you be passing the same bytes to both? Something like: data = myfile.read(bufferSize) data = packets.mkepckt(data, 0, packets.calculateChecksum(data, len(data)))

– Remy Lebeau
Nov 15 '18 at 17:35







I'm no Python expert (I'm not even a Python user), but your calls to sendto() look OK to me. What kind of object is mkepckt() returning exactly? Are you sure it is a byte array? Also, your calls to calculateChecksum() when calling mkepckt() look suspiciously wrong to me. You are calling myfile.read() twice, so you are passing different bytes to mkepckt() and calculateChecksum(). Shouldn't you be passing the same bytes to both? Something like: data = myfile.read(bufferSize) data = packets.mkepckt(data, 0, packets.calculateChecksum(data, len(data)))

– Remy Lebeau
Nov 15 '18 at 17:35















Also, when calling calculateChecksum() after recvfrom(), shouldn't you be using len(ACK) instead of BufferSize? Passing BufferSize to recvfrom() is the maximum number of bytes to read, but that is no guarantee that is the number of bytes actually read, it may be less.

– Remy Lebeau
Nov 15 '18 at 17:39







Also, when calling calculateChecksum() after recvfrom(), shouldn't you be using len(ACK) instead of BufferSize? Passing BufferSize to recvfrom() is the maximum number of bytes to read, but that is no guarantee that is the number of bytes actually read, it may be less.

– Remy Lebeau
Nov 15 '18 at 17:39















Make packet is a tuple. It really just takes all those objects and adds them together into a local variable then returns it. I mostly did it this way so I can have all my "packet" functions in one file, to keep things clean. As for those other two things, I believe you are absolutely correct. I have changed them, thanks. Still getting that int error though, not sure what to do about it.

– DPalmz
Nov 16 '18 at 3:58





Make packet is a tuple. It really just takes all those objects and adds them together into a local variable then returns it. I mostly did it this way so I can have all my "packet" functions in one file, to keep things clean. As for those other two things, I believe you are absolutely correct. I have changed them, thanks. Still getting that int error though, not sure what to do about it.

– DPalmz
Nov 16 '18 at 3:58












1 Answer
1






active

oldest

votes


















0














Thanks to help from @RemyLebeau, I was able to solve this problem by importing pickle and using that to dump my tuple into a variable, then sending that variable as the data. Not sure why the byte data gave off an error about an int, but there you go.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308526%2fpython-sendto-telling-me-it-needs-an-int%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Thanks to help from @RemyLebeau, I was able to solve this problem by importing pickle and using that to dump my tuple into a variable, then sending that variable as the data. Not sure why the byte data gave off an error about an int, but there you go.






    share|improve this answer




























      0














      Thanks to help from @RemyLebeau, I was able to solve this problem by importing pickle and using that to dump my tuple into a variable, then sending that variable as the data. Not sure why the byte data gave off an error about an int, but there you go.






      share|improve this answer


























        0












        0








        0







        Thanks to help from @RemyLebeau, I was able to solve this problem by importing pickle and using that to dump my tuple into a variable, then sending that variable as the data. Not sure why the byte data gave off an error about an int, but there you go.






        share|improve this answer













        Thanks to help from @RemyLebeau, I was able to solve this problem by importing pickle and using that to dump my tuple into a variable, then sending that variable as the data. Not sure why the byte data gave off an error about an int, but there you go.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 '18 at 2:21









        DPalmzDPalmz

        112




        112
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308526%2fpython-sendto-telling-me-it-needs-an-int%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python