Python sendto telling me it needs an int
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
|
show 1 more comment
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
1
What is the EXACT error message? Your calls tosendto()
are inconsistent, though. Some of them are passingdata
directly, while others are passingbytearray(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 tosendto()
look OK to me. What kind of object ismkepckt()
returning exactly? Are you sure it is a byte array? Also, your calls tocalculateChecksum()
when callingmkepckt()
look suspiciously wrong to me. You are callingmyfile.read()
twice, so you are passing different bytes tomkepckt()
andcalculateChecksum()
. 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 callingcalculateChecksum()
afterrecvfrom()
, shouldn't you be usinglen(ACK)
instead ofBufferSize
? PassingBufferSize
torecvfrom()
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
|
show 1 more comment
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
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
python-3.x sockets
asked Nov 14 '18 at 20:51
DPalmzDPalmz
112
112
1
What is the EXACT error message? Your calls tosendto()
are inconsistent, though. Some of them are passingdata
directly, while others are passingbytearray(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 tosendto()
look OK to me. What kind of object ismkepckt()
returning exactly? Are you sure it is a byte array? Also, your calls tocalculateChecksum()
when callingmkepckt()
look suspiciously wrong to me. You are callingmyfile.read()
twice, so you are passing different bytes tomkepckt()
andcalculateChecksum()
. 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 callingcalculateChecksum()
afterrecvfrom()
, shouldn't you be usinglen(ACK)
instead ofBufferSize
? PassingBufferSize
torecvfrom()
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
|
show 1 more comment
1
What is the EXACT error message? Your calls tosendto()
are inconsistent, though. Some of them are passingdata
directly, while others are passingbytearray(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 tosendto()
look OK to me. What kind of object ismkepckt()
returning exactly? Are you sure it is a byte array? Also, your calls tocalculateChecksum()
when callingmkepckt()
look suspiciously wrong to me. You are callingmyfile.read()
twice, so you are passing different bytes tomkepckt()
andcalculateChecksum()
. 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 callingcalculateChecksum()
afterrecvfrom()
, shouldn't you be usinglen(ACK)
instead ofBufferSize
? PassingBufferSize
torecvfrom()
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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 17 '18 at 2:21
DPalmzDPalmz
112
112
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
What is the EXACT error message? Your calls to
sendto()
are inconsistent, though. Some of them are passingdata
directly, while others are passingbytearray(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 ismkepckt()
returning exactly? Are you sure it is a byte array? Also, your calls tocalculateChecksum()
when callingmkepckt()
look suspiciously wrong to me. You are callingmyfile.read()
twice, so you are passing different bytes tomkepckt()
andcalculateChecksum()
. 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()
afterrecvfrom()
, shouldn't you be usinglen(ACK)
instead ofBufferSize
? PassingBufferSize
torecvfrom()
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