Jump to content

Python Socket programming

Hi , I need some help in socket programming, could anyone here tell me how can I make something like a client querying data from the server (maybe a dictionary storing stock and their prices) send data from server to the client (according to the request like stock first and then prices maybe price of multiple stocks at a time). Can I also make the data look in a tabular form as well on the client end?

I'm at the point where I have done simple client-server code did played with dictionary and tried sending it over to the client but comes as a single vertical line not in a tabular form, is this even possible? I'm fairly new to python so..

 

1. How to send rows of data to the client instead of a single vertical line maybe two columns and display like a tabular format to the client?

2. sometimes I do not get all the items in the dictionary stored at the server when I type and send the keyword from the client script to the server script, so i have to type n send the keyword like two times atleast to receive all the items ( Using TCP socket here ).

3. After the first packet I send, the next reply I always receive is of the previous query I sent.

 

New to python.. 

Note: both scripts are on the same machine running python2.7

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, jermanojunior said:

1. How to send rows of data to the client instead of a single vertical line maybe two columns and display like a tabular format to the client?

2. sometimes I do not get all the items in the dictionary stored at the server when I type and send the keyword from the client script to the server script, so i have to type n send the keyword like two times atleast to receive all the items ( Using TCP socket here ).

3. After the first packet I send, the next reply I always receive is of the previous query I sent.

1) You can use a list of lists, or send the data in a single list and interpret it inside the client; for example, if your data is ("fruits", "veggies", "apple", "spinach") you could consider every even index to be one column and every odd index to be another column - or use suitable intervals depending on how many columns you want.

2) Sounds like a bug in the server, can't help you without seeing the code

3) Same as 2)

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

I did use list of lists but got to know they can only exchange packets of strings and not lists so had to convert the list to string and then send, and it outputs just like the dictionary in a single vertical line. Did you mean to change the received string to list again once received?

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, jermanojunior said:

I did use list of lists but got to know they can only exchange packets of strings and not lists so had to convert the list to string and then send, and it outputs just like the dictionary in a single vertical line. Did you mean to change the received string to list again once received?

When I have to send a list as data outside of the scope of the program, I tend to simply append each element of the list separated by a comma. Then it can easily be put back into a list by separating elements at each comma.

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, jermanojunior said:

I did use list of lists but got to know they can only exchange packets of strings and not lists so had to convert the list to string and then send, and it outputs just like the dictionary in a single vertical line. Did you mean to change the received string to list again once received?

I would use json

 


{

    Data: [

        { name: 'LTT', price: 25.67},

        { name: 'YoutTube', price: 35.67}

    ]

}

 

You can data.toString()  the json to sent and then JSON.loads it the other side.

 

That's json from JavaScript I think python requires all keys to be quoted too.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×