knan Light Poster

A service running in the Server is listening to port 2978 on for incoming connections! My PHP code receives URL data through get method. A tcp connection has to be established with the 'service' on that port and the data received from the URL should be sent to the service. The service responds with another data to the same port. this data should be obtained by the code and be saved in a file.

I've written the code. But its showing error! help me pls!!

//fsockopen("tcp://",2978 , $errno, $errstr, 30);
echo urldecode($CONTENT);
$Handle = fopen("/xxx/xxx/OldData.txt", "a");
fwrite($Handle, $CONTENT);
// set IP and Port number
$host = "";
$port = 2978;
set_time_limit(0); // No timeout
$socket = socket_create(AF_INET, SOCK_STREAM, tcp) or die("Could not create socket\n"); // creating a socket
$result = socket_connect($socket, $host, $post) or die ("Could not connect to socket\n"); // connecting socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); // binding socket to port
$result = socket_listen($socket, 1) or die("Could not set up socket listener\n"); // starting to listening for connections
$anothersocket = socket_accept($socket) or die("Could not accept incoming connection\n"); // accept incoming connections and create another socket to handle communication
socket_write($anothersocket, $CONTENT, strlen($CONTENT)) or die ("Could not write output\n");// writing content to port (Input to CS)
$output = socket_read($anothersocket, 1024) or die ("Could not read output from CS\n"); // Getting the output from CS
$Handle1 =fopen("/xxx/xxx/NewData.txt", "a");
fwrite($Handle1, $output);
socket_close($anothersocket); // close sockets ...

knan Light Poster

25 Woman created for success barbie Reynolds sep 1986 Paris Google Limited

From the above XML, i want to get content between [COLOR="Red"]Reynolds[/COLOR]

I want to retrieve Reynolds from tag.

Any help please !! I am using Python

Thanks in advance!

knan Light Poster

Hi, I am building a simple android application that sends GPS coordinates from an Android phone to a Webserver, and the received co-ordinates are displayed one by one in the server. I think both my client and server side programs are correct, but I am not sure. Please correct the code if its not correct.
Where/How can I see the received co-ordinates?

Also I need to see it like this
[CODE]Latitude Longitude
55.17 -3.48
52.18 -2.11
50.23 -1.45[/CODE]

the 1st 55.17 and -3.48 are displayed when the server receives it and both 55.17,-3.48 and 52.18,-2.11 are displayed when the second coordinates are received.

Here is the Android(client)code:
package Firstdroid.Gps;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class UseGps extends Activity
/* Called when the activity is first created. /
public void onCreate(Bundle savedInstanceState)

    /* Using the LocationManager class to obtain GPS locations */

    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

/* Class My Location Listener */
public class MyLocationListener implements LocationListener
    public void onLocationChanged(Location loc)
        String Text = "My current location is: " + "Latitude = " + loc.getLatitude() + "Longitude = "  + loc.getLongitude();
        Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_LONG).show();

        String url = "http://localhost:80/xampp/server.php?lat="+loc.getLatitude()+"&long="+loc.getLongitude();
        try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(url);

            HttpResponse response = httpclient.execute(httppost);
        } catch (Exception e){ ...

(m,n) means m refers n
The expected output is:
how many times m refers to all n which should be put in B
& how many times n is referred which should be put in A

no its not mine!!
I am trying to implement it... but i can only think of the worst case scenario. ie., to create dictionaries from A0 to A9 and from B0 to B9. And then have loops like when i ==1 do this, i ==2 do that....etc... which is very bad...
[CODE] A0={},A1={},.....A9={}

for x in D:
for i = 1 to 10:
if i ==1:
elif i == 9:

    for m in D:
        for n in D:
           if (m,n) in E:
               if i==1:
                   B1[m]=B1[m] + A0[n]
                if i==9:
                    B9[m]=B9[m] + A8[n]
           elif (n,m) in E:
                if i==1:
                if i==9:

print A9,B9[/CODE]

no its not mine!!
I am trying to implement it... but i can only think of the worst case scenario. ie., to create dictionaries from A0 to A9 and from B0 to B9. And then have loops like when i ==1 do this, i ==2 do that....etc... which is very bad...

knan Light Poster

D is a list containing some words. D=[word1,word2,word3......]
E is a list containing combinations of any two words in D ... E=[(word1,word2),(word7,word1)....]
How can I implement the following in python:
[CODE]A0[x]=1 for all x in D # This is actually Ai. when i=0, it becomes A0
B0[x]=1 for all x in D
for i = 1 to 10: # i takes the values 1 to 10
Ai[x]=0 for all x in D
Bi[x]=0 for all x in D
for x in D:
for y in D:
if (x,y) in E:
Bi[x]=bi[x] + Ai-1[q]
if (y,x) in E:

print A10,B10[/CODE]

ow... i was close to this but i dint use that try and except
there is another question for me.

If the dictionary is

How can i make it to,
[CODE]d={'a':{'X':['1','2']}, 'b':{'Y':['1']}}[/CODE]
ie., as a dictionary of dictionaries

Thank you very much!

knan Light Poster

Hi can anyone tell me how to remove empty keys in the following dictionary


I want it to be


I tried this. But its showing error. I know its wrong to change the size of a dictionary during iteration. But is there any other way?
[CODE]>>> for x in d:
... if d[x]==[]:
... del d[x]
Traceback (most recent call last):
File "", line 1, in
RuntimeError: dictionary changed size during iteration[/CODE]

Thank you very much everyone... Now i understand the reason behind it... Thank you!

knan Light Poster

We know that 0.1 + 0.1 + 0.1 - 0.3 = 0.0
But in python 0.1 + 0.1 + 0.1 - 0.3 = 5.5511151231257827e-017

Is there any way I can get 0.0...??

This is great!!! U have integrated both of my question threads and answered here. Very many thanks for that. It gives me more interest to learn the language!!! Thank you very much for your kindness!! :) :)

Yup right! but i want to output that in a sorted manner into a text file.

12 - 123
12 - 187
12 - 1860
12 - 4821
12 - 7000
12 - 9000
18 - 1234
18 - 4000

If i have it like this i can do the above thing easily:

knan Light Poster

I have a dictionary

I want to sort the keys and values such that, the output is,

I tried something! but it didn work
[CODE]>>> for values in dict1:
... dict1[values].sort()

{'18': ['1234', '4000'], '12': ['123', '1860', '187', '4821', '7000', '9000']}[/CODE]

Full sorting happens for values of '18'. The values of '12' are partially sorted.
but '18' and '12' are themselves not sorted. I don't know whats happening!

right! what if in dict2 the key '2' has ['c','d','e']

and in dict1 the key 'e' has ['100','190']

dict3 = {}
for x in dict2:
if len(dict2[x])==2:
key1, key2 = dict2[x]
dict3[x] = list(set(dict1[key1]) & set(dict1[key2]))
elif len(dict2[x])==3:
key1, key2, key3 = dict2[x]
dict3[x] = list(set(dict1[key1]) & set(dict1[key2]) & set(dict1[key3])
print dict3[/CODE]

this will be fine. but what if len(dict2[x]) varies each time...
can the number of lines be reduced?

Yup. ur right. this is what i am getting now.

[CODE]>>> dict1={'a':['100','101','190','76'],'b':['100','176','154','76'],'c':['190','45'],'d':['101','190'],'e':['100']}

for x in dict2:
... for y in dict2[x]:
... if y in dict1:
... dict3[x]=dict[y]
{'1': ['100', '176', '154', '76'], '2': ['101', '190']}[/CODE]

Can u please correct this code???

Now i got another doubt! Please help me with this too!

I have 2 dictionaries. [B]dict[/B] and [B]dict1[/B].
All i have to do is, [U]for each [B]key[/B] in [B]dict1[/B][/U],
I should take its [B]value[/B] and go to [B]dict[/B]. The values are keys in [B]dict[/B]. If the keys in dict have a common value(s), i should output it into a new dictionary.

This is the code i have written so far.
[CODE]>>> test=[]

for x in dict1:
... for y in dict1[x]:
... if y in dict:
... test.append(dict[y])
[['100', '101', '190'], ['100', '176', '154', '76'], ['190', '45'], ['101', '190']][/CODE]

My output should be,


tats awesome! Thank a lot! You are my python god! :)

omg. this is great! Thank you very much. But i have another question. How will the output be if there are words in place of numbers and vice versa.

knan Light Poster

I have a list

[CODE]x = ['abc','1','2','3','def','6','8','5','13','mcg','568','35469','6453'][/CODE]

I want the output file in a dictionary as follows:

[CODE]dict = {'abc':['1','2','3'],'def':['6','8','5','13'],'mcg':['568','35469','6453']}[/CODE]

How can i do this?

[QUOTE=tonyjv;1370509]t-one or t-el? Maybe there would exist clearer variable names? By the way have you learned about list comprehensions?[/QUOTE]


@everyone: yup. that "(" was making the problem.. i was working continuously for 20+ hours and my mind was all f***ed up!!! I had some nice sleep.. and then i could figure this out easily...
Thanks everyone for the help!

knan Light Poster

I have dictionary with single element lists.

[CODE]dictionary={'a':['A'], 'b':['B'], 'c':['C']}
How can i change it to

[CODE]{'a':'A', 'b':'B','c':'C'}[/CODE]

knan Light Poster

I dont know what goes wrong in this piece of code?!!
I keep getting the following error.

File "", line 11
SyntaxError: invalid syntax

[CODE]tl = []
for inlist in token_list:
for token in inlist:

knan Light Poster

I have a list of lists

[CODE]x=[['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h']][/CODE]

I need the following output.


How can i do this? Can a regular expression be used here??

Thank you very much. That was very helpful. How am I going to achieve the 1st and 2nd conditions. I am still trying, but i couldnt figure out a regular expression...

knan Light Poster

I have a file named test.txt
I get the file
print obj






All I want to do with this obj is that, I've to create a regular expression such that,

1.If the left number matches the right number, it should become a single, a=a should become a.

2.Then d=e & e=d means the same. In this case any one of them must be removed. So as for e=f & f=e.

  1. Notice the newlines. Some have \n ,some have \n\n and some have \n\n\n . Make everything into a singe \n for each.

The output should be


Someone please help me coding the regular expression. I've tried to find one for ages, but I could'nt... Help me please.

knan Light Poster

I have a set of text files in a folder. I want to read each file, convert the contents of the file to lowercase, remove punctuations and save them all in another directory with the same filenames. How can i do that???

knan Light Poster

I have 2 lists



I need to make a new list Z such that,

Z = ['ab','ac','ad','ae','af']

I know this uses simple concatenation. I tried several times but without success. I am a beginner so please help me with the code. Thanks in advance.