How to send each csv row one at a time over socket











up vote
0
down vote

favorite












I am writing a code that sends data from client to server. The data I need to send is from a csv file, which has around 1,700 rows. Currently I am sending the data in a large buffer as a whole, but I would prefer to send it row by row and also receive it the same way. I am not sure if I should use the split() function or there are any better ways to do the same thing.



Note: this question is based off the Python socket to send rows of data



I have the exact same problem. The answer given there for the server portion makes no sense to me. Here is that answer repeated here for clarity.



#server.py
def read_line(sock):
return "".join(iter(lambda:sock.recv(1),"n"))


I need to see how to add this answer into the server code listed below.



The server...



import socket
import ipdb

result = ""
HOST = 'LOCAL_IP'
PORT = 42050
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
print "Server running", HOST, PORT
s.listen(5)
conn, addr = s.accept()
print'Connected by', addr

while True:
data = conn.recv(409600)
print repr(data.split("' "))
if not data:
break

print "Done Receiving"
conn.close()


The client...



import socket

HOST = 'SERVER_IP'
PORT = 42050
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))


while True:
for line in open('target.csv', 'rb'):
s.send(line)

break

print "Done Sending"
s.close()


Thank you for your help.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I am writing a code that sends data from client to server. The data I need to send is from a csv file, which has around 1,700 rows. Currently I am sending the data in a large buffer as a whole, but I would prefer to send it row by row and also receive it the same way. I am not sure if I should use the split() function or there are any better ways to do the same thing.



    Note: this question is based off the Python socket to send rows of data



    I have the exact same problem. The answer given there for the server portion makes no sense to me. Here is that answer repeated here for clarity.



    #server.py
    def read_line(sock):
    return "".join(iter(lambda:sock.recv(1),"n"))


    I need to see how to add this answer into the server code listed below.



    The server...



    import socket
    import ipdb

    result = ""
    HOST = 'LOCAL_IP'
    PORT = 42050
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    print "Server running", HOST, PORT
    s.listen(5)
    conn, addr = s.accept()
    print'Connected by', addr

    while True:
    data = conn.recv(409600)
    print repr(data.split("' "))
    if not data:
    break

    print "Done Receiving"
    conn.close()


    The client...



    import socket

    HOST = 'SERVER_IP'
    PORT = 42050
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))


    while True:
    for line in open('target.csv', 'rb'):
    s.send(line)

    break

    print "Done Sending"
    s.close()


    Thank you for your help.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am writing a code that sends data from client to server. The data I need to send is from a csv file, which has around 1,700 rows. Currently I am sending the data in a large buffer as a whole, but I would prefer to send it row by row and also receive it the same way. I am not sure if I should use the split() function or there are any better ways to do the same thing.



      Note: this question is based off the Python socket to send rows of data



      I have the exact same problem. The answer given there for the server portion makes no sense to me. Here is that answer repeated here for clarity.



      #server.py
      def read_line(sock):
      return "".join(iter(lambda:sock.recv(1),"n"))


      I need to see how to add this answer into the server code listed below.



      The server...



      import socket
      import ipdb

      result = ""
      HOST = 'LOCAL_IP'
      PORT = 42050
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind((HOST, PORT))
      print "Server running", HOST, PORT
      s.listen(5)
      conn, addr = s.accept()
      print'Connected by', addr

      while True:
      data = conn.recv(409600)
      print repr(data.split("' "))
      if not data:
      break

      print "Done Receiving"
      conn.close()


      The client...



      import socket

      HOST = 'SERVER_IP'
      PORT = 42050
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.connect((HOST, PORT))


      while True:
      for line in open('target.csv', 'rb'):
      s.send(line)

      break

      print "Done Sending"
      s.close()


      Thank you for your help.










      share|improve this question















      I am writing a code that sends data from client to server. The data I need to send is from a csv file, which has around 1,700 rows. Currently I am sending the data in a large buffer as a whole, but I would prefer to send it row by row and also receive it the same way. I am not sure if I should use the split() function or there are any better ways to do the same thing.



      Note: this question is based off the Python socket to send rows of data



      I have the exact same problem. The answer given there for the server portion makes no sense to me. Here is that answer repeated here for clarity.



      #server.py
      def read_line(sock):
      return "".join(iter(lambda:sock.recv(1),"n"))


      I need to see how to add this answer into the server code listed below.



      The server...



      import socket
      import ipdb

      result = ""
      HOST = 'LOCAL_IP'
      PORT = 42050
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind((HOST, PORT))
      print "Server running", HOST, PORT
      s.listen(5)
      conn, addr = s.accept()
      print'Connected by', addr

      while True:
      data = conn.recv(409600)
      print repr(data.split("' "))
      if not data:
      break

      print "Done Receiving"
      conn.close()


      The client...



      import socket

      HOST = 'SERVER_IP'
      PORT = 42050
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.connect((HOST, PORT))


      while True:
      for line in open('target.csv', 'rb'):
      s.send(line)

      break

      print "Done Sending"
      s.close()


      Thank you for your help.







      python python-2.7 sockets






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 5 at 8:10









      Ashok Kumar Jayaraman

      900715




      900715










      asked Nov 5 at 3:41









      Jessi

      3531618




      3531618





























          active

          oldest

          votes











          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',
          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%2f53148019%2fhow-to-send-each-csv-row-one-at-a-time-over-socket%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148019%2fhow-to-send-each-csv-row-one-at-a-time-over-socket%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini