A service running in the Server is listening to port 2978 on 1xx.xxx.xx.xx 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!!

[CODE]#!/usr/local/php5/bin/php-cgi
<?php
$CONTENT = $_GET["DATA"]."
";
//fsockopen("tcp://1xx.xxx.xx.xx",2978 , $errno, $errstr, 30);
echo urldecode($CONTENT);
$Handle = fopen("/xxx/xxx/OldData.txt", "a");
fwrite($Handle, $CONTENT);
fclose($Handle);
// set IP and Port number
$host = "1xx.xxx.xx.xx";
$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);
fclose($Handle1);
socket_close($anothersocket); // close sockets ...

<text>
<p><s id="s1"><ng><w pws="yes" id="w1" p="CD">25</w> <w l="woman" pws="yes" id="w4" p="NNP" common="true" headn="yes">Woman</w></ng> <vg tense="past" voice="act" asp="simple" modal="no"><w l="create" pws="yes" id="w10" p="VBD" headv="yes">created</w></vg> <pg><w pws="yes" id="w18" p="IN">for</w></pg> <ng><w vstem="succeed" l="success" pws="yes" id="w22" p="NN" headn="yes">success</w> <w l="barbie" pws="yes" id="w30" p="NN" headn="yes">barbie</w></ng> <ng><enamex type="location"><w l="reynold" pws="yes" id="w37" p="NNP" locname="single">Reynolds</w></enamex> <w l="sep" pws="yes" id="w46" p="NN" headn="yes">sep</w></ng> <ng><timex type="date"><w pws="yes" id="w50" p="CD">1986</w></timex></ng> <ng><enamex type="organization"><w l="pari" pws="yes" id="w55" p="NNP" locname="single">Paris</w> <w orgname="single" l="google" pws="yes" id="w61" p="NNP">Google</w> <w l="limited" pws="yes" id="w68" p="NNP" common="true">Limited</w></enamex></ng></s></p>
</text>

=========================================================
From the above XML, i want to get content between <enamex type="location"><w l="reynold" pws="yes" id="w37" p="NNP" locname="single">[COLOR="Red"]Reynolds[/COLOR]</w></enamex>

I want to retrieve Reynolds from tag.

Any help please !! I am using Python

Thanks in advance!

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: UseGps.java
[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.app.Activity;
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. /
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

    /* 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)
    {
        loc.getLatitude();
        loc.getLongitude();
        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);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } 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={}
B0={},B1={},.....B9={}

for x in D:
A0[x]=1
B0[x]=1
for i = 1 to 10:
if i ==1:
A1[x]=0
B1[x]=0
.
.
.
elif i == 9:
A9[x]=0
B9[x]=0

    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:
                    A1[m]=A1[m]+B0[n]
                .
                .
                .
                if i==9:
                     A9[m]=A1[m]+B8[n]

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...

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:
Ai[p]=Ai[p]+Hi-1[q]

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

[CODE]d={'a':[('X','1),('X','2)],'b':[('Y','1)]
[/CODE]
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!

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

d={'a':[],'b':['1','2'],'c':[]}

I want it to be

d={'b':['1','2']}

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 "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration[/CODE]

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

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:
[CODE]dict1={'12':['123','187','1860','4821','7000','9000'],'18':['1234','4000']}[/CODE]

I have a dictionary
[CODE]dict1={'18':['4000','1234'],'12':['7000','4821','187','1860','123','9000']}[/CODE]

I want to sort the keys and values such that, the output is,
[CODE]dict1={'12':['123','187','1860','4821','7000','9000'],'18':['1234','4000']}[/CODE]

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

dict1
{'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']

ie.,
[CODE]dict1={'a':['100','101','190','76'],'b':['100','176','154','76'],'c':['190','45'],'d':['101','190'],'e':['100','190']}
dict2={'1':['a','b'],'2':['c','d','e']}
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']}

dict2={'1':['a','b'],'2':['c','d']}
dict3={}
for x in dict2:
... for y in dict2[x]:
... if y in dict1:
... dict3[x]=dict[y]
...
dict3
{'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=[]

dict={'a':['100','101','190','76'],'b':['100','176','154','76'],'c':['190','45'],'d':['101','190'],'e':['100']}
dict1={'1':['a','b'],'2':['c','d']}
for x in dict1:
... for y in dict1[x]:
... if y in dict:
... test.append(dict[y])
...
test
[['100', '101', '190'], ['100', '176', '154', '76'], ['190', '45'], ['101', '190']][/CODE]

My output should be,

[CODE]dict3={'1':['100','76'],'2':['190']}[/CODE]

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.

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]

t-el!!!!!

@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!

I have dictionary with single element lists.

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

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

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

File "<stdin>", line 11
t1.append(list[temp])
^
SyntaxError: invalid syntax

[CODE]tl = []
for inlist in token_list:
temp=[]
for token in inlist:
temp.append((zlib.adler32(token,65521))
tl.append(temp)[/CODE]

I have a list of lists

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

I need the following output.

[CODE]a-b
a-c
a-d
b-c
b-d
c-d
e-f
g-h[/CODE]

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...

I have a file named test.txt
I get the file
[CODE]file=open("test.txt","r")
obj=file.read()
file.close()
print obj
a=a

b=b

c=c

d=e
e=d

e=f
f=e

f=g
g=h[/CODE]

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 number.ie., 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

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

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

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???

I have 2 lists

X=['a']

Y=['b','c','d','e','f']

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.